PowerShell for SCSM: Copy parent Attachments to Child Activity
What does it do?
This script takes the attachments from the Parent Change, Release or Service Request and copies them to a child Xapity Notification Activity. In the example below I use our Xapity Notification Activity as the target activity, but the script could be easily modified for other activity types.
The script will take all the Parent attachments, but it could also be modified to take specific attachments, for example, with a particular word in the title or of a particular type.
Why do you want to do this?
There are a lot of reasons for wanting to add attachments from a Parent Change or Service Request to an child activity work item:
Service Requests that allow upload of files and then you need to distribute them
Change Requests may need instructions uploaded that you then want to email to others
Release Records have run sheets added that need to be sent to multiple people
This example uses the Xapity Notification Activity - which allows you to send emails from the workflow on a Change, Release or Service Request - as the work item that receives the attachments. Using Xapity PowerShell Activity and Xapity Notification Activity together can produce powerfully results allowing control of when emails are sent and using this script can dynamically add attachments to the email.
High Level Explanation of the Script (available in full below)
As this is designed to work as a script in a Xapity PowerShell Activity the $ParentID variable has already been defined and has the Change, Release or Service Request ID value already set.
The function Add-ActionComment is explained in more detail in the blog post "PowerShell for SCSM: Updating the Action Log".
The next section sets the Parent Class that depends on the work item type, so is specific to Change, Release or Service Request. It also sets the comment type so that at the end of the script we know what to update.
The script defines the Parent Object and the File Attachment Relationship and class. From this it can retrieve the Parent Object attachments and store them in $SourceFiles.
The script now starts the first of two For loops. It is going to loop through each activity on the Parent Object and then use an IF statement to evaluate if that activity matches the criteria for adding attachments. In this example we are looking for the words *AddAttachments* (not case sensitive) in the title of the Activity. I have used a tag of [AddAttachments] on the end of the Activity title. Note: PowerShell will not like using the square brackets "[" or"]" in the search criteria.
Once we have found the activity (and there may be multiple of them on the Parent Object) the script uses the next For loop to add each Attachment to the activity. This is the same as the script in the blog post "PowerShell Scripting: Copy disk Attachments to Work Item". But as this script is specific to the Xapity Notification Activity I have hard coded the Type Projection for Xapity Notification Activity rather than make it a variable depending on the target work item. If you modify this script for other work items you will need to change the Type Projection used.
The last section adds a comment to the ParentObject. This will be an Action Log comment for a Service Request, and a notes field update for a Change or Release.
Full Script: Copy Work Item Attachment from Parent to Child
Download the script: CopyAttachmentToNotificationActivity.zip
Xapity Notification Activity Enables Email Notifications to be sent directly from any System Center Service Manager (SCSM) workflow. With the Xapity Notification Activity, email notifications can have attachments, external and related recipients and can use Notification Templates
Xapity - Innovative Software for SCSM - Discover our Products