Archive for the ‘Stop Runbook w/ Powershell’ Category

Stopping (Canceling) a Runbook Job w/ Powershell   Leave a comment

I’ve moved everything from this blog over to jmattivi.blogspot.com and updated all of the scripts to have straight quotes.  If any don’t work as posted, please let me know!

For further posts please see jmattivi.blogspot.com.

Thanks!

 

Canceling a runbook job requires a few more options in the headers and payload of the request.  There are three properties of the job that you must use to  cancel the runbook.

$JobID = “10000000-0000-0000-0000-000000000000”
$RunbookID = “00000000-1000-0000-0000-000000000000”
$LastModifiedTime = “2012-01-10T13:44:36.823”

First, the url must point to the specific job guid that you intend to stop.

$url = “http://scorch.domain:81/Orchestrator2012/Orchestrator.svc/Jobs(guid’$JobID‘)”

Second, the request headers also require a few more options than a regular GET or POST to start a Runbook.

$request.Headers.Add(“X-HTTP-Method”, “MERGE”)
$request.Headers.Add(“If-Match”, ‘W/”datetime’ + “‘” + $LastModifiedTime + “‘” + ‘”‘)

Third, the payload needs to include these fields in the properties struct:  JobID, RunbookID, and Status = Canceled.

    “<m:properties>” + “`n” +
‘<d:Id m:type=”Edm.Guid”>’ + $JobID + ‘</d:Id>’ + “`n” +
‘<d:RunbookId m:type=”Edm.Guid”>’ + $RunbookID + ‘</d:RunbookId>’ + “`n” +
“<d:Status>Canceled</d:Status>” + “`n” +
“</m:properties>” + “`n” +

Here is the complete script to cancel a specific Job.  Fields in red would need to be updated per your environment.

###############################################################################################################
$creds = Get-Credential(“domain\username“)

$JobID = “10000000-0000-0000-0000-000000000000
$RunbookID = “00000000-1000-0000-0000-000000000000
$LastModifiedTime = “2012-01-10T13:44:36.823

$url = “http://scorch.domain:81/Orchestrator2012/Orchestrator.svc/Jobs(guid’$JobID&#8217;)
$request = [System.Net.HttpWebRequest]::Create($url)
$request.Credentials = $creds
$request.Timeout = 60000
$request.Accept = “application/atom+xml,application/xml”
$request.Headers.Add(“Accept-Charset”, “UTF-8”)
$request.Headers.Add(“X-HTTP-Method”, “MERGE”)
$request.Headers.Add(“If-Match”, ‘W/”datetime’ + “‘” + $LastModifiedTime + “‘” + ‘”‘)
$request.ContentType = “application/atom+xml”
$request.Method = “POST”

$requestBody = ‘<?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?>’ + “`n” +
‘<entry xmlns:d=”http://schemas.microsoft.com/ado/2007/08/dataservices&#8221; xmlns:m=”http://schemas.microsoft.com/ado/2007/08/dataservices/metadata&#8221; xmlns=”http://www.w3.org/2005/Atom”>&#8217; + “`n” +
‘<content type=”application/xml”>’ + “`n” +
“<m:properties>” + “`n” +
‘<d:Id m:type=”Edm.Guid”>’ + $JobID + ‘</d:Id>’ + “`n” +
‘<d:RunbookId m:type=”Edm.Guid”>’ + $RunbookID + ‘</d:RunbookId>’ + “`n” +
“<d:Status>Canceled</d:Status>” + “`n” +
“</m:properties>” + “`n” +
“</content>” + “`n” +
“</entry>”

$requeststream=new-object System.IO.StreamWriter $request.GetRequestStream()
$requeststream.Write($requestBody)
$requeststream.Flush()
$requeststream.Close()

$response=$request.GetResponse()
$requestStream=$response.GetResponseStream()
$readStream=new-object System.IO.StreamReader $requestStream
$Output=$readStream.ReadToEnd()
$readStream.Close()
$response.Close()
###############################################################################################################