SCSM – Get Service Request Object From Any Nested Child Runbook Activity

When you are starting to automate stuff in your cloud environment you probably use SMA for automation and Service Manager to trigger the necessary runbooks in SMA. As this is really great stuff you will start facing many new challenges because you have to script in PowerShell to achieve your goals. Depending on your architecture and your strategy and how you plan your runbooks you will end up with more or the far better way is – less problems.

One strategy I follow is to have just one parameter which is the runbook ID to pass from the runbook activity template to the SMA runbook in Service Manager.  So imagine, you pass the runbook activity ID to your SMA runbook and you want to get some information from the parent work item like the service request. If you have the runbook activity placed directly on the service request itself it is not a problem like in this example…

image

You can write a few lines of code like this….

image

…and the result will look like this…

image

As you can see you will get the ID from the parent work item. In this case the service request SR2067.

But how are you going to proceed if your runbook activity e.g. RB2135 is placed deeply nested in an activity tree because you are building the SCSM activity tree dynamically or you have a very complex template? If you would use the code above you receive the ID from the parent work item in this case the sequential activity SA2134

image

The (theoretical) solution approach is to jump from the runbook activity RB2135 to the parent sequential activity SA2134 and using this activity as starting point to find the next parent work item PA2133 and from there one more step up again and so on until you are at the service request level SR2126. “Nice words bro, but how does this look like in PowerShell?” – Good question buddy.

Here some background information. If an activity is placed on the service request you can use the relation System.WorkItemContainsActivity$ to query the activity. As you can see the relationship has source WorkItem and target Activity. That’s the same relationship we used in the first code sample above…

image

Since you know how to get the parent work item and you know how the relationship works, you need to find some sort of way to build a loop to use the parent work item as input and starting point for iterating one level up the hierarchy.

Here I have written just a few lines of code to solve this challenge…

image

$RunbookActivityID = “RB2135”

#Get the SMA runbook activity class (Cireson SMA connector)
$ActivityClass = Get-SCSMClass -Name System.WorkItem.Activity.SMARunbookActivity$

#Get the SMA runbook activity object
$ActityObject = Get-SCSMObject -Class $ActivityClass -Filter “Name -eq $RunbookActivityID”

#Get the relationship class System.WorkItemContainsActivity$
$RelationWorkItemActivity = Get-SCSMRelationshipClass System.WorkItemContainsActivity$

#Get the relationship object and use the activity as target and query the relationship id for the matching relation id. Select the source object.
$Object = (Get-SCSMRelationshipObject -ByTarget $ActityObject | Where-Object {$_.Relationshipid -eq $RelationWorkItemActivity.id}).SourceObject

While ( $Object.Name -notlike “SR*”)

  {   
      Write-Host -ForegroundColor Green  $Object.Name
      $Object = ((Get-SCSMRelationshipObject -ByTarget $Object | where {$_.Relationshipid -eq $RelationWorkItemActivity.id}).SourceObject)
    
  }

Write-Host -ForegroundColor Yellow  $Object.Name

…and if you run the code it looks like this…

image

Having this code in place we are able to access the service request properties e.g. $Object.Name and also the ID which we are able to use in further runbooks. Of course you could modify the While ($Object.Name –notlike “SR*”) statement to stop at another point in the activity hierarchy like the next sequential activity (SA*) or next parallel activity (PA*).

In this example and In all our other automation projects we use Cireson SMA Connector and the SMLets from Codeplex.

Here I provided the code logic and you just need to fit in a workflow to use it in SMA. Happy SCSM-SMA-ing.

2 Comments

  1. Hi Stefan! Thanks for your your blog post! I’m trying to implement this as sub-workflow in a process I’m experimenting with but getting some strange issues.

    Any time I run Get-SCSMObject inside the SMA environment, it always returns NULL.

    So weird! Any hints you can share on it would be greatly appreciated!

    John

    —-

    Using the below is a simple way to recreate the problem:

    workflow SCSM-Test
    {
    $scsmCredential = get-automationpscredential -name “SCSM Credential”

    $data = InlineScript {
    $smdefaultcomputer = “SM01”

    $class = Get-SCSMClass -Name System.WorkItem.Activity.SMARunbookActivity$
    $obj = Get-SCSMObject -Class $class -Filter “Name -eq ‘RB543′”

    ($obj | convertto-xml).Save(“c:\temp\object.xml”)

    $obj
    } -PSCredential $scsmCredential
    $data
    }

    I end up with the below in the object.xml file:

    • Hi

      It works like that, make sure you use -PSComputerName and target your SCSM server. This example, works you will find your object.xml on the SCSM server in C:\Temp, because we do PowerShell Remoting

      workflow SCSM-Test
      {

      $SCSMServer = Get-AutomationVariable -Name “VARG-SCSMServer”
      $Creds = Get-AutomationPSCredential -Name “VARG-SCSMServerCredential”

      $obj = InlineScript {
      $class = Get-SCSMClass -Name System.WorkItem.Activity.SMARunbookActivity$
      $obj = Get-SCSMObject -Class $class -Filter “Name -eq ‘RB10000′”
      ($obj | convertto-xml).Save(“c:\temp\object.xml”)
      Return $obj
      } -PSCredential $Creds -PSComputerName $SCSMServer
      Return $obj
      }

      Cheers,

      Stefan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s