It could be very helpful to have the description of the parent Change Request in every related Activity (especially the Review and Manual Activities).
One use where this can be helpful is the notification of Reviewers and Implementers by mail with the details in the Change Record description.
To solve this requirement a PowerShell script can be used to write the content of the Description filed of a parent Change Record in every Description field of a related Activity.
A Change Request must be submitted in SCSM 2016 before we start. The required steps to create a Change Request are described in the recipe Working with Change Requests and Release Records in this chapter. We need the ID of the created Change Request, in our example it is "CR575". If you like to run the PowerShell script remotely (not on the SCSM 2016 management server), we need the name of the SCSM 2016 management server as well. In our example, the name of the server is "TDSCSM03".
You must download and install the SCSM PowerShell Cmdlets found at http://smlets.codeplex.com/ (see the Downloading and installing SMLets recipe in Chapter 12, Automating Service Manager 2016).
On the SCSM 2016 management server, start the Windows PowerShell ISE:
# Import SMlets module Import-Module SMlets #------ Variables ------ # ID of the Service Request $id = "CR575" # Name of the SCSM Management Server $smDefaultComputer = "TDSCSM03" #----------------------- # Definition of function to find all related activities function Add-ActivityDescriptionRecursive($activity, $description) { # Set internal activity id $activityId = $activity.Displayname # Get activity object $activity = Get-SCSMObject -Class $activityClass -Filter "Displayname -like $activityId*" # Call function "Add-ActivityDescription" to write description of activity Add-ActivityDescription -activity $activity -description $description # Get related child activities $workItemContainsActivityRelationship = Get-SCSMRelationshipClass -Name System.WorkItemContainsActivity$ # Get related activity objects $recursiveActivities = Get-SCSMRelatedObject -SMObject $activity -Relationship $workItemContainsActivityRelationship # For each related actvity found foreach($obj in $recursiveActivities) { # Call function "Add-ActivityDescription" to write description of activity Add-ActivityDescriptionRecursive -activity $obj -description $description } } # Definition of function function Add-ActivityDescription($activity, $description) { # Exists a description in the Activity? if ($activity.Description) { # Build PropertyHash if Activity Description is not empty $newDescription = $activity.Description + [Environment]::NewLine + "---------------" + [Environment]::NewLine + $description $propertyHash = @{Description = $newDescription} } else { # Set PropertyHash if Activity Description is empty $propertyHash = @{Description = $description} } # Update Description of activity $activity | Set-SCSMObject -PropertyHashtable $propertyHash } # Get Activity and Service Request classes $parentClass = Get-SCSMClass -Name System.WorkItem.ChangeRequest$ $activityClass = Get-SCSMClass -Name System.WorkItem.Activity$ # Get relationship between Service Request and all related Activities $workItemContainsActivityRelationship = Get-SCSMRelationshipClass -Name System.WorkItemContainsActivity$ # Get configured Service Request and its description $parentRequest = Get-SCSMObject -Class $parentClass -Filter "Id = $id" $description = $parentRequest.Description # Get all related Activities of Service Request $activities = Get-SCSMRelatedObject -SMObject $parentRequest - Relationship $workItemContainsActivityRelationship # For each related Activity foreach ($activity in $activities) { # Call function "Add-ActivityDescription" to write description of activity Add-ActivityDescriptionRecursive -activity $activity - description $description }
.ps1
extension to a filesystem location (for example, C: Set-DescriptionActivities.ps1
).
The script contains two functions:
Function 1: Add-ActivityDescriptionRecursive (line 18-36)
Function 2: Add-ActivityDescription (line 37-54)
Line 56-75 gets the parent Change Request details and description, the required classes, and the relationship details
System.WorkItemContainsActivity
.
You can use the same script with minor changes to set the description of all activities related to a Service Request as well:
$parentClass = Get-SCSMClass -Name System.WorkItem.ServiceRequest$