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,
script works fine at detecting Status, at least when Runbook is running. When one is stopped, get the following errors:
ReplyDeletePS C:\Windows\system32> . 'C:\Users\dann.cox\TestScripts\RunbookMonitor\test.ps1'
Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (400) Bad Request."
At C:\Users\dann.cox\TestScripts\RunbookMonitor\test.ps1:54 char:5
+ [System.Net.HttpWebResponse]$response=[System.Net.HttpWebRespo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
new-object : Exception calling ".ctor" with "1" argument(s): "Stream was not readable."
At C:\Users\dann.cox\TestScripts\RunbookMonitor\test.ps1:56 char:19
+ ... $readStream = new-object System.IO.StreamReader $responseStream
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Exception calling "ReadToEnd" with "0" argument(s): "Cannot read from a closed TextReader."
At C:\Users\dann.cox\TestScripts\RunbookMonitor\test.ps1:57 char:5
+ $responseString = $readStream.ReadToEnd()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ObjectDisposedException
and the log file shows:
Runbook MonitorTroubleTicketActionLog | 6d79f547-9037-4ca1-af89-81983c3d2323 - Already running
Runbook MonitorUpdatedIncidents | 43d42feb-e88f-48b2-afc6-39ce045a9401 - Already running
Runbook NewIncidentMonitor | 68c40eb2-16d9-49f8-8657-efa25116cde5 - Already running
Could not start runbook TestMonitor. Status: System.Net.HttpWebResponse.StatusCode
Thanks for the feedback,
DeleteI'll give a look later on my environment and try to check where the bug is :)
Really appreciate your comment, thanks!
This comment has been removed by the author.
DeleteDann,
DeleteThis might solve the issue :)
TRUNCATE TABLE
[Microsoft.SystemCenter.Orchestrator.Internal].AuthorizationCache
EXEC [Microsoft.SystemCenter.Orchestrator.Maintenance].EnqueueRecurrentTask 'ClearAuthorizationCache'
Cheers,
Well, stopping and starting the RunBooks, which my associate had already done, seems to have done the trick, at least for now. If it breaks again I'll the TRUNCATE TABLE business...
DeleteNice to ear it! Thanks for the feedback!
Delete(Yet, Another Blog About ...) System Center: Scorch (Orchestrator) - Runbook Monitor >>>>> Download Now
Delete>>>>> Download Full
(Yet, Another Blog About ...) System Center: Scorch (Orchestrator) - Runbook Monitor >>>>> Download LINK
>>>>> Download Now
(Yet, Another Blog About ...) System Center: Scorch (Orchestrator) - Runbook Monitor >>>>> Download Full
>>>>> Download LINK Kk
unrelated question - all the controls on your blog are in Spanish, although I set my language preference to English. Good thing I have a little Italian in my background... Is this just me?
ReplyDeleteThat's unrelated, but important though. Just change the blog language to EN-US! My bad!
DeleteBtw, it was Portuguese, not Spanish! :)
Cheers from Portugal!
PS: Just came from vacation, and going to check on the bug you found! Hope to solve it soon :)
Thanks! :)
Getting below error when executed the script. Can you please help on this?
ReplyDeleteException calling "GetResponse" with "0" argument(s): "The remote server returned an error: (401) Unauthorized."
At line:64 char:93
+ [System.Net.HttpWebResponse]$response=[System.Net.HttpWebResponse] $Request.GetResponse <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
New-Object : Exception calling ".ctor" with "1" argument(s): "Stream was not readable."
At line:66 char:30
+ $readStream = new-object <<<< System.IO.StreamReader $responseStream
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Exception calling "ReadToEnd" with "0" argument(s): "Cannot read from a closed TextReader."
At line:67 char:45
+ $responseString = $readStream.ReadToEnd <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Praveen,
DeleteHave you changed the credentials on the script, and also the URL of scorch webservice and the request ?
Cheers,
Hi Ricardo,
ReplyDeleteThanks for your response.
Yes, I have changes User, password and updated URL of Scorch in both request and response.
Cheers,
Just admiring your work and wondering how you managed this blog so well. It’s so remarkable that I can't afford to not go through this valuable information whenever I surf the internet! Best 240HZ Monitor 2019
ReplyDelete(Yet, Another Blog About ...) System Center: Scorch (Orchestrator) - Runbook Monitor >>>>> Download Now
ReplyDelete>>>>> Download Full
(Yet, Another Blog About ...) System Center: Scorch (Orchestrator) - Runbook Monitor >>>>> Download LINK
>>>>> Download Now
(Yet, Another Blog About ...) System Center: Scorch (Orchestrator) - Runbook Monitor >>>>> Download Full
>>>>> Download LINK