Retrieving Service Request attachments with Orchestrator

A while ago, Patrik Sundqvist and Anders Bengtsson worked together to publish a PS script to extract files from any workitem in Service Manager. Something you would expect to be easy ends up being a bit complicated. The original script uses smlets. I wanted to use the native SM cmdlets. The translation is not one-to-one, but I was able to create a first version.

Why a first version? Well, it isn’t a 100% generic, since I adapted to the needs I had, which was to run it from Orchestrator, in order to extract the files and attach them to a notification e-mail.

So, a few considerations:

I made it work for the “Service Request” type of WorkItem only, but is easily adaptable. It now works for any WorkItem.

It is looking for the Service Request ID (typically SRxxx). It now uses the real Id to lookup the Service Request object.

The archive folder can be customized in the script and so can the Service Manger server.

Since I’m running this remotely from the SCORCH server, I had to have the SM Console/Shell installed, but it can also be done running a remote shell, which will avoid the –ComputerName on all SCSM commands.

And I have cheated: I added the orchestrator service account to the Service Manager administrators. Don’t do that. Add the credentials (will update in the next version). Smile

So, here is the new version of the script:

#Authors: Patrik Sundqvist, – original version, using SMLets
#Adapted by: Jose Fehse, – to use native Service Manger Commandlets
#Description: Can be used to archive attached files

Import-Module ‘C:Program FilesMicrosoft System Center 2012Service ManagerPowershellSystem.Center.Service.Manager.psd1’
$SMServer=”Your Server Name or variable here”
$SR=Get-SCClassInstance -ComputerName $SMServer –Id <Your ID from the Get SR Activity>
$targetclass=Get-SCSMRelationship -ComputerName $SMServer -DisplayName “Has File Attachment” | where {$_.Source -eq (get-scsmclass -ComputerName $SMServer -Name System.WorkItem)} 
$ArchiveRootPath=”Archive Root path from the Initialize data”
#For each file, archive to entity folder
if($files -ne $Null)
    #Create archive folder
    $nArchivePath = $ArchiveRootPath + “” + $SR.Id
    New-Item -Path ($nArchivePath) -ItemType “directory” -Force|Out-Null
                $fileobject=get-scsmclassinstance -ComputerName $SMServer -Id $fileId
                $fs = [IO.File]::OpenWrite(($nArchivePath + “” + $_.EnterpriseManagementObject.DisplayName))
                $memoryStream = New-Object IO.MemoryStream
                $buffer = New-Object byte[] 8192
                while (($bytesRead = $fileobject.Content.Read($buffer, 0, $buffer.Length)) -gt 0)
                    $memoryStream.Write($buffer, 0, $bytesRead)


How to use in Orchestrator.

1. Create a New Runbook in your RB library. I called it Extract SR attachments.

2. Add an Initialize data activity:


3.Add a Get Object activity, to retrieve the object. Since we’re in orchestrator, I had to specify the class: Service Request.

image You can create one for Incidents, changes, etc.

4.Add the script:


That’s how it should look:


Final Runbook:


To test it, I have used an existing Runbook activity from a workflow I used the other day. I could have just passed the parameters manually, but it is more interesting like this:


The extract files Activity calls my runbook:


After executing, the folder is created, with the files in them.

I have also added a list of the files that were extracted, with a full path in the filelist returned data:


Will look like that:


(yes, there is NL/CR there,believe me)

More than that, have I separated the three first files, in case you want to pick them directly. With that, you can send the info directly to a sendmail Activity (but be careful: this activity requires you to specify every attachment file and if it is empty, it will fail). I would try to send the e-mails using powershell as well (maybe in another post).

My test:




For the next version of the runbook, I intend to publish the list of variables, so you can fetch the files and attach them to a notification e-mail. DONE!

Have fun!