Neat Trick to Get SCSM Service Request Attachments

Have you ever wished that you could get the attachments out of an SCSM work item without having to go into the console and dig them out?

I know I did.

With the almighty power of Google, I found a few PowerShell scripts that do this, and I thought I would share how I have used some of that code. In this example I am using a SCORCH Runbook to call this PS script. This way we can leverage the Runbook in other automation activities.

First, an example or two of how this can be used.

Let’s say we have an SCSM Request Offering published via the portal. In this RO, the user can attach a document (let’s call it a work order). Normally, the analysts processing this request would have to dig it out of the SCSM work item manually. By utilizing this Runbook , we can automate storing the attachment in a network folder. Perhaps this folder is monitored by your document management system, or maybe it’s just a central repository for these documents.

Another way you might use this is to email that attachment to another party via Runbook automation. The user submits the attachment, the job gets logged, the Runbook kicks off, the attachment is stored in a temporary folder and then attached to an email sent from SCORCH.

So how do we do this?

An overview of the Runbook (pretty simple hey)


The Initialize Data activity has some fields that we might like to pass along to the following activities. You can plan this ahead of time to best suit the way you wish to use it. You will need one for the SC Object GUID of the Service Request. I’ve also added a field for Destination and the friendly ID of the work item (e.g.. SR123456). The script creates a folder with that ID to store the files in. Your Runbook will need to know this ID to move that folder after the script has run. You could pass the variable out of the script to the Runbook , but if you are already parsing the SR’s SC Obj GUID, it’s easy enough to also pass through the SR ID.

The .net activity is a PowerShell script, and it is going to invoke a PSSession on the SCSM server. You could also use the Execute PowerShell Script activity with the appropriate details of your SCSM Server.

Here is the script.

$Session=New-PSSession -ComputerName “scsmserver”
Invoke-Command -Session $session -ScriptBlock
Import-Module ‘C:\Program Files\Microsoft System Center 2012\Service Manager\Powershell\System.Center.Service.Manager.psd1’
$SR=Get-SCClassInstance -ComputerName $SMServer –Id ‘{SC Object GUID from “Initialize Data”}’
$targetclass=Get-SCSMRelationship -ComputerName $SMServer -DisplayName “Has File Attachment” | where {$_.Source -eq (get-scsmclass -ComputerName $SMServer -Name System.WorkItem)}
#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)

Remove-PSSession $Session

What that has done is copied any attached files from our Service Request to C:\Temp\OrchestratorRemote\(SR ID) on the SCSM Server. You can alter that path to whatever suits.

The last activity in the Runbook is going to move that folder to the path specified in Initialize Data. Add a “move folder” activity, with the source as \\scsmserver\c$\temp\orchestrator and the destination path as {Destination from “Initialize Data”}

Now, if you ever need to get attachments out of work items – you can just invoke this Runbook and off it goes.



    • KaiTingey

      Hi Vit

      It’s a fairly simple activity, the only real requirement is the SC Object GUID of the service request. I have also added an entry for the ‘normal’ Service Request ID (eg. SR123456) and another entry for the destination. If you set it up the same as what I have, all you need to do is pass the SC object guid, the desired destination and the SR ID fields.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s