Kicking off SMA Runbooks from SCSM Part 4: Monitor for Multiple Runbooks

Author by Duncan Lindquist

In the last blog post we reviewed how to monitor for a single runbooks in SMA. To take burden of your runbook worker service as well as efficiency you will want to monitor for multiple runbooks at the same time. To do this we are going to configure the base of the monitor the same as we did in the Creating the SMA Monitor post. This should give us a starting monitor runbook that looks like this. 12- SMA Runbook1   The first thing that we are going to do is get the Run Book Activities in SCSM that are in progress and have not been run. To do this we are going to create a new runbook to gather these. The runbook will use the following PowerShell (copy paste version at the end of the post). 27-RBAS   Now that we have a runbook to get the runbook activities for SCSM we will need one to get the SR that contain these runbooks. When we kick off the runbook to get the SR we will pass it the Activity ID so we will set this as a parameter at the start of the runbook. 28-Param Next we will use the following PowerShell to get the Service Request (copy paste version at the end of the post). 29- SR Runbook   Since we are passing a variable into an inline script in this workbook you will notice we are using the $using variable to pass the Runbook ID into the inlinescript instance. 30- using Now that we have both these runbooks created we can go back to our monitor runbook and start kicking of our rubooks! The first thing we are going to do in the while($true) loop is get our runbooks to kick off. To do this we are going to call our SMA runbook to get these and set them to a variable. 31- WhileTrue The next step will be a foreach loop that will run for each activity kicking of the appropriate runbook. The first step in the foreach runbook will be to get the Service Request that relates to the runbook so we can use that information later in the runbook we kick off. 32- For Each   Now that we have our RB and SR information we can kick off our runbooks. To do this we will use a switch statement base of the runbook name. We can get the runbook name easily by using $RB.EXTSMARunbooks. The PowerShell will look like this. 33- switch   Now if we put it all together we are monitoring for our runbooks and kicking them off! The final runbook will look like this. 34-MultiMonitor   Now that we have our monitors figured out we need to deal with failed runbooks and completing the runbooks. These will be covered in the next blog posts. As promised below are the Scripts Used in a copy paste format. Get SCSM Runbook Activities
workflow Get-SCSMRunbookActivities { $RBS = inlinescript { Import-Module smlets#Get Class $RBAClass = Get-SCSMClass -Name microsoft.systemcenter.orchestrator.runbookautomationactivity.base #Get Object $RBAObject = get-SCSMobject -Class $RBAClass #Get Runbook Automation Activities that are In Progress $SMARBA = $RBAObject | where {($RBAObject.status.displayname -eq "In Progress") -and ($RBAObject.EXT_SMARunbookStatus.Displayname -eq "Not Run")} $SMARBA | Set-SCSMObject -Property EXT_SMARunbookStatus -Value Running $SMARBA | select ID, ExtSMARunbooks, EXT_SMARunbookStatus } -pscomputer scsm $RBS }
  Get SCSM Service Requests
workflow Get-SCSMServiceRequests { param( [string] $RBID ) $SRInfo = 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.ID -eq $using:RBID} #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 $} $S } $SMARBA | Set-SCSMObject -Property EXT_SMARunbookStatus -Value Running $SRFinal | select ID, ExtString01, ExtString02, ExtString03 } -pscomputer scsm $SRInfo }
Final Monitor Runbook
workflow Base-Monitor { <#Paramaters MonitorFrequency - The interval in seconds that you want to monitor SCSM #> param ( [int]$MonitorFrequency )$StartTime = Get-Date while($true) { $RBS = Get-SCSMRunbookActivities foreach ($RB in $RBS) { $SR = Get-SCSMServiceRequests -RBID $RB.ID Switch -CaseSensitive ($RB.EXTSMARunbooks) { "Test" { Test -String1 $SR.ExtString01 } "Testing_1" { Testing_1 -String1 $SR.ExtString02 } } } start-sleep $MonitorFrequency } }

Duncan Lindquist

Service Management & Automation Solution Lead