Kicking off SMA Runbooks from SCSM Part 3: Monitor for a Single Runbook

Author by Duncan Lindquist

Now that we have created our monitor runbook we need something to monitor for. In this post we will walk through how to monitor for a single runbook. The next post will cover how to monitor for multiple runbooks. To monitor for a single runbook we will need to put a script inside of our monitor to get the runbook we want. We will walk through the script in pieces then put it together at the end. Since all SMA Runbooks are run in a work flow we are going to be using an Inline Script to set a variable to contain an array of all the Service Request containing the runbooks and mapped information that we want. To do this the first line of PowerShell in our monitor will look like this: 20- Inline Script Next we need to import the smlet module. 21- Import Module Now that we have the module we need first thing that we will need to do is get the classes of the objects we will be looking for. In this case it will be the Runbook Activity and its Related Service Request. For those of you familiar with Orchestrator this will be doing the same task as the class field in the get-object and monitor-object activities. 13.1 - Orch Get Classes   To do this in PowerShell we will use the following lines of code to set the classes to variables. 13- Get Classes Now that we have the relation ship types we need to get the objects to do this we will run the following PowerShell. 16- Objects Powershell Now that we have the objects we will need to get the relationship class for the relationship between Runbook Automation Activity and Service Request object. For people familiar with Orchestrator this would be doing the same task as the get-relationship. 14-Get Relationship   To do this in PowerShell we will use the following lines of code to set the class to a variable. 15- PS Get Relationship The next step is to get the specific Runbook objects we are looking for. To do this we are going to set a few filters. The first is going to be on the runbook status we want to make sure the runbook is set to "In Progress" this way we know all activities before have been completed. The second filter will be checking our SMA Runbook field this will filter it down to the specific runbook we are looking for (in the next post I will cover how to do multiple). Lastly we need to filter on our SMA Runbook status field that we created we want to make sure that it is set to "Not Run" so that we don't pick up anything that is currently running. We will do all this with the following PowerShell 18- Filter Now that we have the Runbook Activities that we want to get the related Service Request to do that we will run the following PowerShell. 19- Get Service Requests Now that we have all the information we are almost ready to kick off our runbook. Before we do this lets set the status to running so we don't grab it again on the next loop. To do this we use the following PowerShell. 22- Running Last step is to get the information we need to kick of our runbook. In my case I mapped information I need to my first three extension fields. So I gather the data with this PowerShell. 23- SR Final   Now I need to wrap up my inline script to set the variable $ServiceRequest. To do this I will add the bracket to the end } and a -pscomputer. The use of the -pscomputer it to tell SMA what server to run the inline script against. Since I am grabbing information from Service Manager I am running it against my Management Server. 24- PS computer   Now that we have all of the information we need to kick of our runbook in SMA we need to tell it to run. Since Service Request is an array with all the service request in it we will need to do a foreach loop to kick of the runbook for each service request. In this example the SMA runbook I want to kick off is SMARunbookTest 26- new for each Once you wrap the code in your monitor it should look like script below. Stay tuned for the next blog post on monitoring for multiple runbooks.

workflow Monitor-SCSM {<#Paramaters MonitorFrequency - The interval in seconds that you want to monitor SCSM #> param ( [int]$MonitorFrequency )$StartTime = Get-Datewhile($true) { Checkpoint-Workflow $NewStartTime = Get-Date $ServiceRequest = inlinescript { Import-Module smlets #Get Classes $SRclass = get-scsmclass -name system.workitem.servicerequest$ $RBAClass = Get-SCSMClass -Name microsoft.systemcenter.orchestrator.runbookautomationactivity.base #Get Objects $RBAObject = get-SCSMobject -Class $RBAClass $SRObject = Get-SCSMObject -Class $SRClass #Get Relationships $SRRelationship = Get-SCSMRelationshipClass system.workitemcontainsactivity$ #Get Runbook Automation Activities that are In Progress and for the Server Build Request $SMARBA = $RBAObject | where {($RBAObject.status.displayname -eq "In Progress") -and ($RBAObject.ExtSMARunbooks.Displayname -eq "Monitor-SCSM") -and ($RBAObject.EXT_SMARunbookStatus.Displayname -eq "Not Run")} #Get The Service Request information $SRID = foreach ($RB in $SMARBA) { $RBs = Get-SCSMRelationshipObject -TargetRelationship $SRRelationship -TargetObject $RB $SRs = $RBs.SourceObject $SRs } $SRFinal = foreach ($SR in $SRID) { $S = $SRObject | where {$_.ID -eq $SR.name} $S } $SMARBA | Set-SCSMObject -Property EXT_SMARunbookStatus -Value Running $SRFinal | select ID, ExtString01, ExtString02, ExtString03 } -pscomputer scsm foreach($Request in $ServiceRequest) { SMARunbookTest -Ext1 $ServiceRequest.ExtString01 -ext2 $ServiceRequest.ExtString02 start-sleep $MonitorFrequency } }

 

Author

Duncan Lindquist

Service Management & Automation Solution Lead