Over the web many people have written already how to achieve it using PowerShell or WebService. My attempt is just to club all required pieces and simplifies for any newbie who is attempting it first time.

Alright, we need Runbook Server Name, Runbook Id, and RunBook Parameters GUIDs to invoke any runbook using PowerShell. Here I am listing some of the powershell scripts which you can get it from Microsoft sites also.

Below script can help you to invoke runbook-

Reference URL: https://msdn.microsoft.com/en-us/library/hh921685.aspx

# Details of the runbook we are going to run
$rbid = "754ac2bc-7642-40a8-9621-aada45541e4e"

$rbParameters = @{"a1ce2001-2f3b-4ccc-960a-1e848841b479" = "2";"402804e2-c541-4990-abce-2a1cbd010a23"="";"afac3efb-7db0-4398-9c97-4e912a853d3b" = "COMP";"d88d9118-1746-4d80-9ddb-574d24d37586"="Server2012";"481e8b2f-5bf6-4c58-a95e-a3009c530af8"="XYZ";"8166b8e0-1a09-4c6c-b369-bd78c7fdd1ac"="";"09f7ea72-c7bf-49f0-bf40-c7d350c0536c"="masrivastava@xyz.com";"4d03734d-803e-4192-a51d-e6ffe4edf165"="8"}

# Create the request object
$request = [System.Net.HttpWebRequest]::Create("http://XYZVMM1:81/Orchestrator2012/Orchestrator.svc/Jobs")

#you must replace XYZVMM1 with your Runbook Server Name.

# 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 ("",$RunbookServers,"")
}

# Format the Runbook parameters, if any
$rbParamString = ""
if ($rbParameters -ne $null) {

# Format the param string from the Parameters hashtable
$rbParamString = "<![CDATA["
foreach ($p in $rbParameters.GetEnumerator())
{
$rbParamString = -join ($rbParamString,"{",$p.key,"}",$p.value,"")
}
$rbParamString += "]]>"
}

# Build the request body
$requestBody = @"

$rbid
$rbserverstring
$rbparamstring

"@

# 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()

Write-Host $response.StatusCode

# 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
}

Note:

  • $rbid variable should contain your runbook id which you can get using below given script.
  •  $rbParameters variable contains the parameters which are required for invoking the parameters.
  • Every parameters (Optional and Required) of runbook are must to be present in the script under $rbParameters variable with and without value.
  • $rbParameters variable is containing Runbook parameters GUIDs and their values.

Below script help us to get Runbook Parameters GUIDs plus runbook id. Its written by Denis Rougeau

# Configure the following variables
$RunbookName = "XYZ"
$SCOWebProt = 'http'
$SCOWebSvc = 'XYZ'
$SCOWebPort = '81'
$UseDefaultCreds = $True  # $False, you'll be prompt for credentials

function QuerySCOWebSvc
{ Param ([string] $url)

$textXML = ""

# Get the Request XML
$SCOrequest = [System.Net.HttpWebRequest]::Create($url)
$SCOrequest.Method = "GET"
$SCOrequest.UserAgent = "Microsoft ADO.NET Data Services"

# Set the credentials to default or prompt for credentials
if ($UseDefaultCreds -eq $true)
{ $SCOrequest.UseDefaultCredentials = $true }
Else
{ $SCOrequest.Credentials = Get-Credential }

# Get the response from the request
[System.Net.HttpWebResponse] $SCOresponse = [System.Net.HttpWebResponse] $SCOrequest.GetResponse()

# Build the XML
$reader = [IO.StreamReader] $SCOresponse.GetResponseStream()
$textxml = $reader.ReadToEnd()
[xml]$textxml = $textxml
$reader.Close()

Return $textxml

Trap {
Write-Host "-> Error Querying Orchestrator Web Service."
Return ""
}
}

# Main
$RbkGUID = ""
$RbkName = ""
$RbkPath = ""
$ParamName = ""
$ParamGUID = ""

# Get the Runbook GUID for the specified Runbook Name
$urlrunbook = "$($SCOWebProt)://$($SCOWebSvc):$($SCOWebPort)/Orchestrator2012/Orchestrator.svc/Runbooks?`$filter=Name eq '$RunbookName'"
$runbookxml = QuerySCOWebSvc $urlrunbook

# Get all the entry nodes in case more then 1 match
$RunbookEntries = $runbookxml.getElementsByTagName('entry')
if ($RunbookEntries.count -eq 0) { Write-Host "-> Runbook Not Found" }

foreach ($RunbookEntry in $RunbookEntries)
{
$RbkGUID = $RunbookEntry.GetElementsByTagName("content").childNodes.childnodes.item(0).innerText
$RbkName = $RunbookEntry.GetElementsByTagName("content").childNodes.childnodes.item(2).innerText
$RbkPath = $RunbookEntry.GetElementsByTagName("content").childNodes.childnodes.item(9).innerText
Write-Host "$RbkGUID ($RbkPath)"

# Get list of Parameters for the Runbook
$urlrunbookparam = "$($SCOWebProt)://$($SCOWebSvc):$($SCOWebPort)/Orchestrator2012/Orchestrator.svc/Runbooks(guid'$RbkGUID')/Parameters"
$runbookxmlparam = QuerySCOWebSvc $urlrunbookparam

# Get all the entry nodes
$ParamEntries = $runbookxmlparam.getElementsByTagName('entry')
if ($ParamEntries.count -eq 0) { Write-Host "-> Runbook Parameters Not Found" }

foreach ($ParamEntry in $ParamEntries)
{
$ParamGUID = $ParamEntry.GetElementsByTagName("content").childNodes.childnodes.item(0).innerText
$ParamName = $ParamEntry.GetElementsByTagName("content").childNodes.childnodes.item(2).innerText
Write-Host "$ParamGUID ($ParamName)"

} # Loop ParamEntries
} # Loop RunbookEntries

Note:

  • $RunbookName = “XYZ” and $SCOWebSvc = ‘XYZ’ values must be replaced with your Runbook name and Runbook Server Name.
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s