• Glen (Xapity)

PowerShell for SCSM: Pull Data via Relationship to Work Item



Why do you want to do this? Quite often you will need to get data from the another work item and a Service Manager relationship will

provide the connection between the objects. Relationships have a source and target object and we can start from either side and still use the relationship to connect to the other object.

When starting from the source object you can use the Get-SCSMRelatedObject to connect to the target. This would be an example of a "push" of data from the source to the target. But this does not work in reverse, when starting from the target.

When starting from the target object, you can use the Get-SCSMObject and Get-SCSMRelationshipObject to get to the source object as shown in the script below. This is an example of a "pull" of data from the source to the target.

The example below can be used by the Xapity PowerShell Activity that is placed on a Change, Release or Service Request template that is used by the Xapity Create Activity to create work items. Add it as the first activity on the workflow to copy data from the Parent work item to the new created work item.

What does it do? In this example we have a Change that was created via a Xapity Create Activity work item on a Service Request. That is, a Service Request was raised (via the console or portal) that had a Create Activity that created a Change from a template.

The Relationship that connects the two objects is Xapity.CreateActivity.DependsOnWorkItem. This is a custom one to one relationship that comes with the Xapity Create Activity.

We are starting from the Change work item that was created by the Create Activity. As a result we have the target of the relationship and not the source. The SMLets commandlet Get-SCSMRelatedObject will not work in this case and we can use the Get-SCSMObject and Get-SCSMRelationshipObject commandlets to get to the source object of the relationship.

The end result is that we will have the Parent Object (a Service Request in this example) of the Create Activity that created the Change that we started from. This now allows you to copy data from the Service Request to the Change, including the user data that was captured on the Service Request via the portal.

High Level Explanation of the Script (download below) The script defines the starting object, in this case a little confusingly in a variable called $ParentID. Using this variable makes the script compatible with a Xapity PowerShell Activity that could be the first activity in the Change workflow.

The next steps define the variables, classes and relationships that are used in the script. For $ParentRelationshipID we are using the out of the box Parent Relationship and can thus use the GUID directly.

Next, we use the Xapity custom Relationship to get the Create Activity. As we know this is a one to one relationship that was used to create the Change, we also know that there can only be one Create Activity at the end of it. The Change object is the target on the relationship, so we use the Get-SCSMRelationshipObject setting the target parameters and use brackets around it to then get the Source Object ID. Then use the Get-SCSMObject commandlet to get the Source Create Activity object.

Now we have the source object, but it is the Create Activity that was used in the Service Request workflow and we actually want the Parent Service Request object. We will use the While loop described in the blog post "How to Get the Parent Work Item of an SCSM Activity" to find the Parent Source object.

And finally we append the ID of the Source Parent object to the Change Object we started with. This could be extended to bring any data you need across - user input, Description etc.

Full Script

Download the script: PullDataviaRelationship.zip


Read more PowerShell for SCSM blogs


Xapity PowerShell Activity

The Xapity PowerShell Activity is a new 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

#PowerShell #CreateActivity #PowerShellActivity #Tips

© 2018 Xapity PTY LTD ABN: 81 611 883 482

  • White RSS Icon
  • White YouTube Icon
  • White Vimeo Icon
  • Facebook Clean
  • Twitter Clean