How to run Orchestrator Runbook using PowerShell?

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:

# 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"="";"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

# 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 = @"



# Create a request stream from the request
$requestStream = new-object System.IO.StreamWriter $Request.GetRequestStream()

# Sends the request to the service

# 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

Write-Host $response.StatusCode

# Get the ID of the resulting job
if ($response.StatusCode -eq 'Created')
$xmlDoc = [xml]$responseString
$jobId = $
Write-Host "Successfully started runbook. Job ID: " $jobId
Write-Host "Could not start runbook. Status: " $response.StatusCode


  • $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 }
{ $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

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


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

How to update app.config file using PowerShell?

Below code will help to update app.config file with given data-

#It helps to connect db and get the data.

$HostName = $env:computername
$connectionstring = 'Server=XX.XX.XX.XX;Database=TestDataBase;User Id=VM;Password=VMTest;MultipleActiveResultSets=True'
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$query = "SELECT [Params].value('(/root//Version/node())[1]', 'nvarchar(max)') as FirstName from Request where [Params].value('(/root//Name/node())[1]', 'nvarchar(max)') = '"+$HostName+"'"
$command = $connection.CreateCommand()
$command.CommandText = $query
$result = $command.ExecuteReader()
$table = new-object “System.Data.DataTable”
$Unified = $table | %{ $_.FirstName }

#It helps to update app.config file with given data.

$appConfigFile = 'C:\Project\ProjectPath\Project.dll.config'
$doc = (Get-Content $appConfigFile) -as [Xml]
$node = $doc.configuration.appSettings.add | where {$_.Key -eq 'Version'}
$node.value = $Unified

How to convert PowerShell Object to a String?

Alike any other languages, PowerShell script also supports conversion of an object to a string for many kinds of manipulations. In my course of actions, I have come across two ways which help to convert Object to a string very easily.


$string = Get-CimInstance Win32_OperatingSystem | Select-Object {$_.Version} | Out-String


$string = Get-CimInstance Win32_OperatingSystem | %{$_.Version}

Note: you can replace your parameter with ‘Version’