Created
May 6, 2020 20:06
-
-
Save 808codist/c17a203a891b8701bf84d08426df5fd6 to your computer and use it in GitHub Desktop.
start SCORCH runbook
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
# source: http://msdn.microsoft.com/en-us/library/hh921685.aspx | |
# just in case above URL goes bye-bye | |
# | |
# Details of the runbook we are going to run | |
$rbid = "00000000-0000-0000-00000000000000001" | |
$rbParameters = @{"00000000-0000-0000-00000000000000002" = "This is the value for Param1.";" 00000000-0000-0000-00000000000000003" = " This is the value for Param2."} | |
# Create the request object | |
$request = [System.Net.HttpWebRequest]::Create("http:// server01.contoso.com:81/Orchestrator2012/Orchestrator.svc/Jobs") | |
# Set the credentials to default or prompt for credentials | |
$request.UseDefaultCredentials = $true | |
# $request.Credentials = Get-Credential | |
# Build the request header | |
$request.Method = "POST" | |
$request.UserAgent = "Microsoft ADO.NET Data Services" | |
$request.Accept = "application/atom+xml,application/xml" | |
$request.ContentType = "application/atom+xml" | |
$request.KeepAlive = $true | |
$request.Headers.Add("Accept-Encoding","identity") | |
$request.Headers.Add("Accept-Language","en-US") | |
$request.Headers.Add("DataServiceVersion","1.0;NetFx") | |
$request.Headers.Add("MaxDataServiceVersion","2.0;NetFx") | |
$request.Headers.Add("Pragma","no-cache") | |
# If runbook servers are specified, format the string | |
$rbServerString = "" | |
if (-not [string]::IsNullOrEmpty($RunbookServers)) { | |
$rbServerString = -join ("<d:RunbookServers>",$RunbookServers,"</d:RunbookServers>") | |
} | |
# Format the Runbook parameters, if any | |
$rbParamString = "" | |
if ($rbParameters -ne $null) { | |
# Format the param string from the Parameters hashtable | |
$rbParamString = "<d:Parameters><![CDATA[<Data>" | |
foreach ($p in $rbParameters.GetEnumerator()) | |
{ | |
#$rbParamString = -join ($rbParamString,"<Parameter><ID>{",$p.key,"}</ID><Value>",$p.value,"</Value></Parameter>") | |
$rbParamString = -join ($rbParamString,"<Parameter><ID>{",$p.key,"}</ID><Value>",$p.value,"</Value></Parameter>") | |
} | |
$rbParamString += "</Data>]]></d:Parameters>" | |
} | |
# Build the request body | |
$requestBody = @" | |
<?xml version="1.0" encoding="utf-8" standalone="yes"?> | |
<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom"> | |
<content type="application/xml"> | |
<m:properties> | |
<d:RunbookId m:type="Edm.Guid">$rbid</d:RunbookId> | |
$rbserverstring | |
$rbparamstring | |
</m:properties> | |
</content> | |
</entry> | |
"@ | |
# Create a request stream from the request | |
$requestStream = new-object System.IO.StreamWriter $Request.GetRequestStream() | |
# Sends the request to the service | |
$requestStream.Write($RequestBody) | |
$requestStream.Flush() | |
$requestStream.Close() | |
# Get the response from the request | |
[System.Net.HttpWebResponse] $response = [System.Net.HttpWebResponse] $Request.GetResponse() | |
# Write the HttpWebResponse to String | |
$responseStream = $Response.GetResponseStream() | |
$readStream = new-object System.IO.StreamReader $responseStream | |
$responseString = $readStream.ReadToEnd() | |
# Close the streams | |
$readStream.Close() | |
$responseStream.Close() | |
# Get the ID of the resulting job | |
if ($response.StatusCode -eq 'Created') | |
{ | |
$xmlDoc = [xml]$responseString | |
$jobId = $xmlDoc.entry.content.properties.Id.InnerText | |
Write-Host "Successfully started runbook. Job ID: " $jobId | |
} | |
else | |
{ | |
Write-Host "Could not start runbook. Status: " $response.StatusCode | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment