Home Orchestrator

Copying File Attachments from one SR to another?

Aaron_SmithAaron_Smith Customer IT Monkey ✭
Has anyone been able to use Orchestrator or even powershell to take a FileAttachement from an SR and Attach to another?

Best Answer

Answers

  • Geoff_RossGeoff_Ross Cireson Consultant O.G.
    Hi Aaron,

    I've managed it indirectly in PowerShell. Copying file attachments out to a folder somewhere and then reimporting them into a new work item. I found the solution in two half's searching online. It wasn't super simple but see how you get on.

    Geoff
  • Aaron_SmithAaron_Smith Customer IT Monkey ✭
    I've found quite a few custom scripts, but have been unable to get them to function. This is one of them:


    Import-Module ‘D:\Program Files\Microsoft System Center 2012 R2\Service Manager\Powershell\System.Center.Service.Manager.psd1’
    $SMServer = ”<servername>”
    $SR = Get-SCClassInstance -ComputerName $SMServer | where {$_.Id -eq "SR68848"}
    $targetclass = Get-SCSMRelationship -ComputerName $SMServer -DisplayName “Has File Attachment” | where {$_.Source -eq (get-scsmclass -ComputerName $SMServer -Name System.WorkItem.ServiceRequest$)} 
    $files = $SR.GetRelatedObjectsWhereSource($targetclass)
    $ArchiveRootPath = ”D:\Attachments”
    #For each file, archive to entity folder
    $filelist = @()
    if($files -ne $Null)
    {
        #Create archive folder
        $nArchivePath = $ArchiveRootPath + “” + $SR.Id
        New-Item -Path ($nArchivePath) -ItemType “directory” -Force|Out-Null
     
        $files|%{
                Try
                {
                    $filelist += ”$nArchivePath$_”
                    $fileId = $_.EnterpriseManagementObject.Id
                    $fileobject = get-scsmclassinstance -ComputerName $SMServer -Id $fileId
                    $fs = [IO.File]::OpenWrite(($nArchivePath + “” + $_.EnterpriseManagementObject.DisplayName))
                    $memoryStream = New-Object IO.MemoryStream
                    $buffer = New-Object byte[] 8192
                    [int]$bytesRead|Out-Null
                    while (($bytesRead = $fileobject.Content.Read($buffer, 0, $buffer.Length)) -gt 0)
                    {
                        $memoryStream.Write($buffer, 0, $bytesRead)
                    }       
                    $memoryStream.WriteTo($fs)
                }
                Finally
                {
                    $fs.Close()
                    $memoryStream.Close()
                }
        }
    }
    $file1=$filelist[0]
    #$file2=$filelist[1]
    #$file3=$filelist[2]
Sign In or Register to comment.