PowerShell for SCSM: Download Attachments
This script is based on the work of Patrik Sundqvist, www.litware.se from the blog post
Sometimes you want to copy the attachments from a Service Request or Change to a network drive. This may be at the beginning of the workflow where the attachment is used by the analysts to complete the activity or it might be at the end of a workflow to archive the attachments or add them to the electronic records management system. This could be done at in bulk on an interval, but it is easier to just schedule it in the workflow when you want it to run. Also add this to Xapity PowerShell Activity to copy attachments.
This script will copy all attachments from the parent work item and place them on a local drive, but it could be a network drive if required and that the workflow account has permissions to.
High Level Explanation of Copy Attachments Script
This is a high level description of what the script is doing. Again thanks to Patrik for posting about how to extract the attachments from a work item.
Step 1: Start with importing SMLets Smlets can be downloaded from here: https://smlets.codeplex.com/
Step 2: Attachment Path Define where the attachments will be stored. This script will create a folder with the Work Item ID as its name e.g. C:\Temp\CR404
Step 3: Get the Attachments information This retrieves the information about all the attachments on the parent work item Define the Parent object as
Step 4: Create folder Creates the folder to hold the files =, but only if there is at least one attachment on the work item i.e. files has some data in it. And I like to get output on my scripts, so have written in the folder name.
Step 5: Copy the Attachments This piece of code is directly out of Partrik's blog http://blogs.litware.se/?p=1369 and copies the attachments from the work item and writes them to the drive location.
It is looping through all the files that were found on the work item. The script is creating a new object in memory and then is reading the file out of the work item into memory. It then writes the memory stream to disk.
The buffer variable is used by memory to chunk up the attachments as they are copied, but is not limiting the size of the attachments.
I have added a Write-host at the end so that it outputs the name of each attachment that is copied to the drive.
The Full Script
Download this script: DownloadAttachments.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