Last active
April 9, 2018 01:10
-
-
Save richardszalay/6f03d8898bf524d4ea26ba99333e80e6 to your computer and use it in GitHub Desktop.
Invoke-SitecoreCommerceBootstrap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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