PowerShell for SCSM: How to get the parent Work Item of an SCSM Activity
Why do you want to do this? Sometimes you have the ID of an activity and need to get the Parent Change, Release or Service
Request. This can be very useful if you are using Xapity Create Activity and want to get data from the Parent Work Item. Or you may have your own reasons for wanting to get the Parent object of an Activity. Once you have the Parent work item you can do anything you like, set properties, copy properties or even add other activities.
What does it do? Searching on the internet I found the LazyWinAdmin (Francois-Xavier Cat) blog which is a really good resource for PowerShell examples. Francois-Xavier provides a great function to get the parent work item from an Activity ID (or GUID), but it assumes that the activity is NOT nested. See his Blog Post here: http://www.lazywinadmin.com/2016/02/powershellscsm-get-work-item-parent-of.html
The main part of his function uses the out of the box Parent Relationship that exists on the activity to find the level above.
But if the Activity is in a container (Parallel, Sequential) it will only provide the container ID. So I have extended the example above to loop until it reaches a Change, Release or Service Request (you could also easily extend it for Incidents if required).
High Level Explanation of the Script (download below) In the script below I have set the Activity class to the high level System.Workitem so that we do not have to worry if it is a Parallel, Sequential, Review, Manual, Dependent or a custom activity like Xapity Create Activity or Xapity PowerShell Activity. This simplifies the script as we do not need to know exactly what type of activity we have.
The script uses a While loop to go through all the potential levels. The $ChildActivity variable being set to "No" is the trigger to stop the loop. It also has a count check that was useful during testing when it would get into an infinite loop (this could now be removed).
The Script uses the Parent Relationship to get then level above work item. This could be a container activity or the Parent CR, RR, or SR we are after. So we check against the class of the work item to see if it matches Service Request first, then see if it is a Change and then a Release. If it matches one of there we set the variable $ChildActivity to No and break the loop.
The output at the end lists the direct properties on the Parent work item and is only used as an example for this blog.
In a production environment, I would make this script a function that accepts the Activity ID as the input and has the Parent Object has an output. And depending on how structured your environment is, I would add some error handling for when it gets data it does not expect. Depending on your use, it may also need to be extended to include support for incidents.
Download Script: GetParentWorkItem.zip
Xapity PowerShell Activity
The Xapity PowerShell Activity is an Activity for SCSM that enables PowerShell scripts to be run in SCSM. Automate more of your processes directly in SCSM. Scripts are stored and updated from a central script repository. The scripts execution output can be viewed and if a failure occurs, an email notification can be sent.
Xapity Create Activity
Xapity Create Activity allows major Work Item types to be created from SCSM Change Requests, Release Records and Service Requests. For example, you can create a Change Request from a Service Request activity workflow and wait for it to be completed before progressing.
Xapity - Innovative Software for SCSM - Discover our Products