Using SMLets Beta 3 Post #2–Using Get-SCSMEnumeration, Get-SCSMRelationshipObject, Get-SCSMRelationshipClass to Automatically Resolve Incidents When All Child Activities Are Completed
Published Feb 15 2019 05:56 AM 2,487 Views
First published on TECHNET on Apr 21, 2011

This is a continuation in a series of blog posts on using SMLets Beta 3.  Previous blog posts in this series:

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.

Here is the script:

Import-Module SMLets

$ActivityID = "MA37139"

$Activity = Get-SCSMObject -Class (Get-SCSMClass -Name System.WorkItem.Activity.ManualActivity$) -Filter "ID -eq $ActivityID"

$Incident = (Get-SCSMRelationshipObject -ByTarget $Activity | ?{$_.RelationshipID -eq "2da498be-0485-b2b2-d520-6ebd1698e61b"}).SourceObject

$ChildActivities = (Get-SCSMRelationshipObject -BySource $Incident | ?{$_.RelationshipID -eq "2DA498BE-0485-B2B2-D520-6EBD1698E61B"})

$CountOfIncompleteActivities = 0

$Completed = Get-SCSMEnumeration -Name ActivityStatusEnum.Completed

$ChildActivities | %{$Activity = Get-SCSMObject -ID $_.TargetObject.Id; if($Activity.Status -ne $Completed){$CountOfIncompleteActivities++}}

if($CountOfIncompleteActivities -eq 0){$Resolved = Get-SCSMEnumeration -Name IncidentStatusEnum.Resolved;$Incident | Set-SCSMObject -Property Status -Value $Resolved}

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 blog post .

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:

SourceObject      : TIR37127 - Test Incident #2
TargetObject      : MA37139: Test Activity 2.1
RelationshipId    : 2da498be-0485-b2b2-d520-6ebd1698e61b
IsDeleted         : False
Values            : {}
LastModified      : 4/21/2011 19:11:05
IsNew             : False
HasChanges        : False
Id                : fd7e612d-ccf9-7822-caff-7fe027f5a765
ManagementGroup   : FABRIKAM
ManagementGroupId : a8a613f1-772d-d860-25c7-7d2413fce9de

So – now we have the source (parent in this case) object – the incident.  Now we need to get all 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 array of activities stored in $ChildActivities.

Next we need to set a counter for the number of in 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 ( not 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.

Version history
Last update:
‎Mar 11 2019 08:43 AM
Updated by: