Instantly share code, notes, and snippets.

Embed
What would you like to do?
Invoke-SitecoreCommerceBootstrap
function Invoke-SitecoreCommerceBootstrap {
param(
[string]$Username = "sitecore\admin",
[string]$Password = "b",
[string]$IdentityServer = "https://localhost:5050",
[string]$OpsServer = "https://localhost:5015"
)
$ErrorActionPreference = "Stop"
$resp = Invoke-WebRequest `
-Uri "$IdentityServer/connect/token" `
-Body @{
"client_id" = "postman-api";
"scope" = "openid EngineAPI postman_api";
"grant_type" = "password";
"username" = $Username
"password" = $Password
} `
-Method "POST"
$authResponse = ConvertFrom-Json $resp.Content
$accessToken = $authResponse.access_token
try
{
$response = Invoke-WebRequest `
-Uri "$OpsServer/commerceops/Bootstrap()" `
-SessionVariable SitecoreCommerceBootstrapSession `
-Headers @{
"Authorization" = "Bearer $accessToken"
} `
-ErrorAction SilentlyContinue `
-Method "OPTIONS" | Out-Null
} catch {
Write-Error "Engine initialization failed, most likely caused by an issue with bootstrap\Global.json (malformed JSON or a serialized type from a missing assembly). Check the logs for more information"
return
}
$xsrfTokenCookie = $SitecoreCommerceBootstrapSession.Cookies.GetCookies($OpsServer)["XSRF-TOKEN"]
$bootstrapHeaders = @{ "Authorization" = "Bearer $accessToken" }
if ($xsrfTokenCookie) {
$bootstrapHeaders["X-XSRF-TOKEN"] = $xsrfTokenCookie.Value
}
try {
$resp = Invoke-WebRequest `
-Uri "$OpsServer/commerceops/Bootstrap()" `
-WebSession $SitecoreCommerceBootstrapSession `
-Headers $bootstrapHeaders `
-Method "PUT"
} catch {
Write-Error "Bootstrap failed, most likely caused by an issue with environment JSON (malformed or a serialized type from a missing assembly). Check the logs for more information"
return
}
$bootstrapResponse = ConvertFrom-Json $resp.Content
if ($bootstrapResponse.ResponseCode -eq "Error") {
$bootstrapResponse.Messages.Text | Write-Error -ErrorAction Continue
return
}
}
Export-ModuleMember Invoke-SitecoreCommerceBootstrap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment