Filling in all related Activity Descriptions with Descriptions from parent Change Requests

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.

Getting ready

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).

How to do it...

On the SCSM 2016 management server, start the Windows PowerShell ISE:

  1. Type the following code into a new script window in 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 
          } 
    
  2. Save the file as a PowerShell file with a .ps1 extension to a filesystem location (for example, C: Set-DescriptionActivities.ps1).
  3. Modify line 9 and line 12 in the script with your CR ID and the SCSM 2016 management server name (marked in yellow).

    How to do it...

  4. Press F5 or click on Run Script in Windows PowerShell ISE.
  5. Verify in the Output window of Windows PowerShell ISE that there is no error message.
  6. Verify the result in the SCSM 2016 console | Work Items | Change Management by opening the Change Request (CR575 in our example) and related Activities on Activities tab. The result should look like this in all activities related to the Change Request:

    How to do it...

How it works...

The script contains two functions:

Function 1: Add-ActivityDescriptionRecursive (line 18-36)

  • This function discovered all related activities of a Change Request recursive (if you have nested activities such as Parallel Activities containing other related activities)
  • A second function is called (Add-ActivityDescription) for each discovered related activity

Function 2: Add-ActivityDescription (line 37-54)

  • This functions checks if a description in the related activity already exists. If yes, a new line and the Description of the parent Change Request is added. If there is no content in the Description field of the activity only the Description of the Change Request is added.

Line 56-75 gets the parent Change Request details and description, the required classes, and the relationship details System.WorkItemContainsActivity .

There's more...

Setting the Description for all activities related to a Service Request

You can use the same script with minor changes to set the description of all activities related to a Service Request as well:

  • Modify line 9 with a Service Request ID, for instance, SR453
  • Modify line 57 with the class of Service Request: $parentClass = Get-SCSMClass -Name System.WorkItem.ServiceRequest$
  • Run the script

    Setting the Description for all activities related to a Service Request

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.237.255