Xapity Blog

PowerShell for SCSM: Copy Parent Description to Child Activities

Copy Parent Details to Child Activities in an SCSM workflow

In the video promoting the Xapity PowerShell Activity we showed a script that copied the parent

description field to the children activities. We thought it might be useful to others and help get you started with running PowerShell scripts in your workflows. This can be adapted to copy additional fields from the parent and is only one way to approach the problem.

The script makes use of the Xapity PowerShell Activity variable $ParentID. If you are using this without the Xapity PowerShell Activity you will need to replace this variable with the ID of the work item you are targeting e.g. CR40402.

High Level Explanation of Copy Parent Description to Children Activities

Step 1: Import SMLets Import SMLets using the -Force switch to make sure they are loaded. Smlets can be downloaded from here: https://smlets.codeplex.com

Step 2: Determine if the Parent is a CR or RR and then set the Parent Class We use an IF statement to check if the Parent Work Item is a Change or Service Request. This then sets the correct Class that is used further down or outputs that we have a bad class for error checking.

Step 3 : Optional: Write out data gathered The script is a bit verbose, but it does help when initially coding to make sure that it is returning what you expect.

Step 4 : Get the SCSM Object We then take the Xapity PowerShell Activity inbuilt variable $ParentID with the Class from above and use the Get_SCSMObject command to populate the $Parent variable.

Step 5 : Optional: Write out data gathered Again just to check that we are returning what you expect.

Step 6 : Get all Activities related to the Parent Work Item Now we get the activities that are related to the parent work item using Get-SCSMRelatredObjects. We have filtered this to Activity work items of all types (RA, MA, SA, PA, DA) using the TypeName property. Use the -Depth Recursive switch to go through all levels of activities, otherwise it will only retrieve the first level of activities.

Step 7 : Set the Description on Each Activity Item The foreach loop below goes through each of the activities found above and writes the Activity ID and Title to output. To get the correct data to display we needed to put the property information $activity.ID in quotes with the dollar at the front. We pipe the activity to the Set-SCSMObject to set the description to the parent description. The write host statement is optional is just there as a output check.

Step 8 : Optional: Finish And just to be complete output that the script is complete. I like Verbose remember... also, Xapity PowerShell Activity will store any of you Write-Host lines for review later.


Extending to Append rather than Overwrite

If you wanted to append the parent description rather than overwrite it, then you would modify the script with the code below. It checks to see if the activity description is empty and if it is, writes the parent description (same as overwrite above). Otherwise it adds the parent description to the activity description, using "`n" to add a new line between the Activity and Parent Descriptions. Using "`r`n" would add a blank line between them (carriage return + new line).


Download these scripts: CopyDescription.zip

The Full Script (Overwrite)

The Full Script (Append)


Xapity PowerShell Activity - Run PowerShell Scripts directly in SCSM workflows

Xapity PowerShell Activity

The Xapity PowerShell Activity is a new Activity for SCSM that enables PowerShell scripts to be run in SCSM workflows. 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 - Innovative Software for SCSM - Discover our Products

#PowerShell #PowerShellActivity #Tips #SCSM