Saturday, June 2, 2012

Stopping (Canceling) a Runbook Job w/ Powershell

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')"
$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" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">' + "`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()
#######################################################################################################

2 comments:

  1. How do I obtain the $JobID, $RunbookID and $LastModifiedTime information using Powershell ?

    ReplyDelete
    Replies
    1. Hi Wyatt,

      I have another post which details getting these attributes....

      https://jmattivi.blogspot.com/2012/06/get-all-running-runbook-jobs.html

      You would use this url to specify returning those fields....

      $url = "http://scorch.domain:81/Orchestrator2012/Orchestrator.svc/Jobs()?`$filter=Status eq 'Running'&`$select=Id,RunbookId,LastModifiedTime,Status"

      You could also filter the specific runbook you're looking for with this....

      $url = "https://scorch.domain:81/Orchestrator2012/Orchestrator.svc/Jobs()?`$filter=Status eq 'Running' and RunbookId eq guid'10000000-0000-0000-0000-000000000000'&`$select=Id,RunbookId,LastModifiedTime,Status"

      Hope that helps,
      Jon

      Delete