So i've made this Powershell script to put on schedule tasks or somewhere else (perhaps a Operations Manager monitor - i'll do it later! :) )
The script :
<#
If for some reason you get the following PS error :
Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (400) Bad Request."
Please log-in into Orchestrator Database and run :
TRUNCATE TABLE [Microsoft.SystemCenter.Orchestrator.Internal].AuthorizationCache;
EXEC [Microsoft.SystemCenter.Orchestrator.Maintenance].EnqueueRecurrentTask ‘ClearAuthorizationCache’
#>
'Starting monitoring ... ' >> C:\Powershell\Log\RunbookManager.log
# Runbook names here :)
$OpsMgrRunbooks =@('1-Runbook','2-Runbook','3-Any-other-Runbook-Name')
$user = 'your_orchestrator_user'
$pass = ConvertTo-SecureString 'your_password' -AsPlainText -Force
$creds = New-Object System.Management.Automation.PsCredential($user,$pass)
foreach ($runbook in $OpsMgrRunbooks) {
$url = "http://YOUR_ORCHESTRATOR_SERVER:81/Orchestrator2012/Orchestrator.svc/Jobs()?`$expand=Runbook&`$filter=(Runbook/Name eq '$runbook')&`$select=Runbook/Name,Status"
$request = [System.Net.HttpWebRequest]::Create($url)
$request.Credentials = $creds
$request.Timeout = 120000
$request.ContentType = 'application/atom+xml,application/xml'
$request.Headers.Add('DataServiceVersion', '2.0;NetFx')
$request.Method = 'GET'
$response = $request.GetResponse()
$requestStream = $response.GetResponseStream()
$readStream=new-object System.IO.StreamReader $requestStream
$Output = $readStream.ReadToEnd()
$readStream.Close()
$response.Close()
$Output > $env:TEMP\1.log
$htmlid = Get-Content -Path $env:TEMP\1.log | Select-String -pattern '<id>.*Runbooks.*'
$bookid = ($htmlid -split "'")[1]
$status = $Output -match "<d:Status>Running</d:Status>"
If ($Status -ne $True) {
$request = ''
$request = [System.Net.HttpWebRequest]::Create("http://YOUR_ORCHESTRATOR_SERVER:81/Orchestrator2012/Orchestrator.svc/Jobs")
$request.Credentials = $creds
$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")
$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">$bookid</d:RunbookId>
</m:properties>
</content>
</entry>
"@
$requestStream = new-object System.IO.StreamWriter $Request.GetRequestStream()
$requestStream.Write($RequestBody)
$requestStream.Flush()
$requestStream.Close()
[System.Net.HttpWebResponse]$response=[System.Net.HttpWebResponse] $Request.GetResponse()
$responseStream = $Response.GetResponseStream()
$readStream = new-object System.IO.StreamReader $responseStream
$responseString = $readStream.ReadToEnd()
$readStream.Close()
$responseStream.Close()
if ($response.StatusCode -eq 'Created') {
$jobId = ([xml]$responseString).entry.content.properties.Id.InnerText
"Successfully started runbook: $rubook. Job ID: $jobId" >> C:\Powershell\Log\RunbookManager.log
}
else { "Could not start runbook $runbook. Status: $response.StatusCode" >> C:\Powershell\Log\RunbookManager.log }
}
Else { "Runbook $runbook | $bookid - Already running" >> C:\Powershell\Log\RunbookManager.log }
}
You'll get the following output :
Runbook 1.Runbook_Testing | 6063dfb2-0f86-4b49-bc38-72a9bef9239c - Already running
Runbook 1.2 SCOM Close Alert | 0d51fb6e-0112-4e91-84f1-a713cd5fd1ae - Already running
Runbook 1 - AlertForwarding | 22585473-7906-4ca4-b4e3-121657cb2e42 - Already running
Successfully started runbook. Job ID: 0a1fdc16-dce4-4585-a2a2-a3da273a704e
Successfully started runbook. Job ID: ee345fc4-f21b-4d05-be8c-c47249ceec36
Hope you find it useful :)
Cheers,