PowerShell for SCSM: Copy Attachments from Disk to a Work Item
What does it do?
This script takes the contents of a folder and attaches all the files to a Change, Release or Service
Request. The folder can be located on the SCSM Workflow server or on a network share, but make sure the SCSM workflow account has permissions to the folder.
The source folder is hardcoded in the script and it would be possible to make this more dynamic.
There are a number of ways to do this and a couple would be:
Extend the SR, RR or CR to have a property for source files and then use this field as the source folder location
Have a set of folders hard coded in the script and then use a keyword or property to determine which source folder to use. This might work well with software deployments
Why do you want to do this?
There are a number of reasons for wanting to add attachments to a work item:
Software Requests on SR's you may want to add software guides
Change Requests you may want to add build sheets
Release Record add the release notes or run sheets from disk
The Script example is for Change, Release and Service Requests, but can be modified to support any work item with the related items functionality.
Note on Manual Activities the default form does not display Related Items.
High Level Explanation of the Script (available in full below)
The first section Sets the variables that are used in the script. basically the folder path to get the attachments and the FileAttachment class and relationship. These are the same for Change, Release and Service Requests.
This script is designed to work in a Xapity PowerShell Activity the $ParentID variable has already been defined and will have the Change, Release or Service Request ID value already set.
The script retrieves the files from the source folder. This could be made more dynamic and could have error checking added to see if the folder was empty.
The next section sets the variables that depend on the work item type, so are 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 function Add-ActionComment is explained in more detail in the blog post "PowerShell for SCSM: Updating the Action Log". The actual main work of the script is done in the ForEach loop. After retrieving the files from the folder, the script attaches each file to the ParentID work item. It does this by:
Reading the file into a File Stream
Creating a new SCSM object and setting the properties of the object
Setting the appropriate Type Projection for the ParentID work item.
The Type Projection is used when the new file object is add to SCSM and the ones used are available out of the box. The Type Projection defines relationships that exist on the ParentID and allow the object to be correctly added to SCSM.
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 Change and Release.
Download the script: CopyAttachmentFromDisk.zip
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