I was talking to a customer today and they wanted to have a workflow which would automatically resolve incidents when all of their child manual activities were completed. Great opportunity for Opalis or using SMLets in a SCSM workflow! I chose to do it with SMLets so I could show you some of the new cmdlets.
And a screenshot of it from my computer that is easier to read:
So – let’s take it apart….
First – import the SMLets module. The fun always begins with importing SMLets!
Next – for testing purposes I created an incident with a couple of child manual activities. I put one of the child manual activity IDs in here. After we are satisfied with the script we would want to load this up in the authoring tool and kick this off automatically and pass in the activity work item ID whenever the status changes.
Next we need to get the Activity object by passing the ID to Get-SCSMObject. For more details on this cmdlet and its companion Get-SCSMClass see the previous
Now that we have the ID that just had its status change we need to traverse up the relationship to the parent incident. To do that we first need to get all the objects that are related to the activity. Then we can pipe that to a ? (alias for Where-Object) that looks at the relationship ID property. If it matches the relationship ID for the WorkItemContainsActivity relationship type then we know we have the parent incident.
How did I know the GUID? I just inspected the results that came back from this:
So – now we have the source (parent in this case) object – the incident. Now we need to get
of the target (child in this case) activity work items. We can do that by using Get-SCSMRelationshipObject again but this time pass the incident object as the Source. Then we filter by the same relationship type ID. The result is an
of activities stored in $ChildActivities.
Next we need to set a counter for the number of
complete activities. We’ll start at zero and count how many are incomplete. If the number never goes above zero then we know that they are all complete.
Next we get the ‘Completed’ enumeration using Get-SCSMEnumeration. How did I know the name of the enumeration? I just used Get-SCSMEnumeration to find it using the term ‘Completed’ as a search term! Like this:
Now we need to pipe the ChildActivities array to a for-each loop (% is the alias for for-each).
In this case each object in the ChildActivities array is not a full object – it doesn’t have the .Status property on it so we need to get the full object by calling Get-SCSMObject and passing the ID (GUID) of the object.
Once we have the full object we can test to see if the status is equal to completed. If it is then increment our incomplete activities counter.
After we have looped through each child activity we need to evaluate whether the count of incomplete activities is zero. If it is then that means that all manual activities in this incident are completed and therefore the status of the incident should be changed to resolved.
To do that we just get the Resolved incident status enumeration. Then we pipe the incident object to the super powerful Set-SCSMObject cmdlet and tell it to change the Status property and set the value equal to the Resolved enumeration.
Now what you would really want to do is hook this up to a workflow using the SCSM Authoring Tool. The workflow criteria would be that whenever the manual activity work item status changed FROM Not Equal to Completed TO Equal to Completed. Then you would drop in a PowerShell script activity into the workflow. Then paste in the PowerShell script above.
Then you would want to replace the line
$ActivityID = "MA37139"
With something more like
$ActivityID = $ID
and then bind the $ID variable to the Work Item ID property (
the ID (Internal) property).
Leave some comments with suggestions on what you would like to see how to do with SMLets and I’ll try to use them as examples.