locked
Add File Attachment to CI using Powershell RRS feed

  • Question

  • Hi Everyone,

    does sombody have a sample Powershell script of how to add an file attachment to a CI ? Found a few c# samples, but need to use Powershell.

    I want to add Icon files to a CI as File attatchment.

    Thanks

    Friday, February 21, 2014 5:52 PM

Answers

  • Hi Sebastian,

    Try this:

    # --- load smlets ---
    $a = (get-module|%{$_.name}) -join " "
    if(!$a.Contains("SMLets")){Import-Module SMLets -ErrorVariable err -Force}
    
    $managementGroup = new-object Microsoft.EnterpriseManagement.EnterpriseManagementGroup "localhost"
    $FileAttachmentRel = Get-SCSMRelationshipClass "System.ConfigItemHasFileAttachment"
    $classFA = Get-SCSMClass -name "System.FileAttachment"
    
    $Folder = "C:\temp\temp2"
    $File = get-childitem $folder | ?{$_.name -like 'test.txt'}
    $CItp = Get-SCSMObjectProjection System.ConfigItem.Projection -filter "DisplayName -eq 'Win7'"
    
    $filepath = $Folder +"\" + $File.Name
    #------------ Inject attach --------------------------------------------------------------------
    $mode =[System.IO.FileMode]::Open
    $fRead = new-object System.IO.FileStream $filepath, $mode
    $length = $file.length
    $newFileAttach = new-object Microsoft.EnterpriseManagement.Common.CreatableEnterpriseManagementObject($managementGroup, $classFA)
    
    $newFileAttach.Item($classFA, "Id").Value = 		[Guid]::NewGuid().ToString()
    $newFileAttach.Item($classFA, "DisplayName").Value = 	$File.Name
    $newFileAttach.Item($classFA, "Description").Value = 	$File.Name
    $newFileAttach.Item($classFA, "Extension").Value = 	$File.Extension
    $newFileAttach.Item($classFA, "Size").Value = 		$length
    $newFileAttach.Item($classFA, "AddedDate").Value = 	[DateTime]::Now.ToUniversalTime()
    $newFileAttach.Item($classFA, "Content").Value = 	$fRead
    
    $CItp.__base.Add($newFileAttach, $FileAttachmentRel.Target)
    $CItp.__base.Commit()
    
    $fRead.close()


    Cheers,
    Marat
    Site: www.scutils.com  Twitter:   LinkedIn:   Facebook:   

    Sunday, February 23, 2014 2:39 AM

All replies

  • Hi Sebastian,

    Try this:

    # --- load smlets ---
    $a = (get-module|%{$_.name}) -join " "
    if(!$a.Contains("SMLets")){Import-Module SMLets -ErrorVariable err -Force}
    
    $managementGroup = new-object Microsoft.EnterpriseManagement.EnterpriseManagementGroup "localhost"
    $FileAttachmentRel = Get-SCSMRelationshipClass "System.ConfigItemHasFileAttachment"
    $classFA = Get-SCSMClass -name "System.FileAttachment"
    
    $Folder = "C:\temp\temp2"
    $File = get-childitem $folder | ?{$_.name -like 'test.txt'}
    $CItp = Get-SCSMObjectProjection System.ConfigItem.Projection -filter "DisplayName -eq 'Win7'"
    
    $filepath = $Folder +"\" + $File.Name
    #------------ Inject attach --------------------------------------------------------------------
    $mode =[System.IO.FileMode]::Open
    $fRead = new-object System.IO.FileStream $filepath, $mode
    $length = $file.length
    $newFileAttach = new-object Microsoft.EnterpriseManagement.Common.CreatableEnterpriseManagementObject($managementGroup, $classFA)
    
    $newFileAttach.Item($classFA, "Id").Value = 		[Guid]::NewGuid().ToString()
    $newFileAttach.Item($classFA, "DisplayName").Value = 	$File.Name
    $newFileAttach.Item($classFA, "Description").Value = 	$File.Name
    $newFileAttach.Item($classFA, "Extension").Value = 	$File.Extension
    $newFileAttach.Item($classFA, "Size").Value = 		$length
    $newFileAttach.Item($classFA, "AddedDate").Value = 	[DateTime]::Now.ToUniversalTime()
    $newFileAttach.Item($classFA, "Content").Value = 	$fRead
    
    $CItp.__base.Add($newFileAttach, $FileAttachmentRel.Target)
    $CItp.__base.Commit()
    
    $fRead.close()


    Cheers,
    Marat
    Site: www.scutils.com  Twitter:   LinkedIn:   Facebook:   

    Sunday, February 23, 2014 2:39 AM
  • Hi Marat,

    first, thanks for the Script.

    I have a Problem with the line:

    $CItp.__base.Add($newFileAttach, $FileAttachmentRel.Target)
    $CItp.__base.Commit()

    Here my script

    # --- load smlets ---
    $a = (get-module|%{$_.name}) -join " "
    if(!$a.Contains("SMLets")){Import-Module SMLets -ErrorVariable err -Force}
    
    $managementGroup = new-object Microsoft.EnterpriseManagement.EnterpriseManagementGroup "localhost"
    $FileAttachmentRel = Get-SCSMRelationshipClass "System.ConfigItemHasFileAttachment"
    $classFA = Get-SCSMClass -name "System.FileAttachment"
    
    $Folder = "C:\"
    $File = get-childitem $folder | ?{$_.name -like 'test.txt'}
    $CItp = Get-SCSMObjectProjection System.ConfigItem.Projection -filter "DisplayName -like 'cemadtm-dc01'"
    
    $filepath = $Folder +"\" + $File.Name
    #------------ Inject attach --------------------------------------------------------------------
    $mode =[System.IO.FileMode]::Open
    $fRead = new-object System.IO.FileStream $filepath, $mode
    $length = $file.length
    $newFileAttach = new-object Microsoft.EnterpriseManagement.Common.CreatableEnterpriseManagementObject($managementGroup, $classFA)
    
    $newFileAttach.Item($classFA, "Id").Value = 		[Guid]::NewGuid().ToString()
    $newFileAttach.Item($classFA, "DisplayName").Value = 	$File.Name
    $newFileAttach.Item($classFA, "Description").Value = 	$File.Name
    $newFileAttach.Item($classFA, "Extension").Value = 	$File.Extension
    $newFileAttach.Item($classFA, "Size").Value = 		$length
    $newFileAttach.Item($classFA, "AddedDate").Value = 	[DateTime]::Now.ToUniversalTime()
    $newFileAttach.Item($classFA, "Content").Value = 	$fRead
    
    $CItp.__base.Add($newFileAttach, $FileAttachmentRel.Target)
    $CItp.__base.Commit()
    
    $fRead.close()
    

    Can you help me please?

    Thanks for your Time

    Wednesday, March 26, 2014 4:18 PM
  • Expanded this into a Function to work with Workitems and ConfigItems.

    Function Add-SCSMFileAttachment
    {
        <#
        .SYNOPSIS
            Add one or more file attachments to an SCSMObject (WorkItem or ConfirItem)
    
        .DESCRIPTION
            Add one or more file attachments to an SCSMObject (WorkItem or ConfirItem)
    
        .PARAMETER <ParameterName>
            <Parameter Decription>
        
        .EXAMPLE
            #Files to Objects
            Add-SCSMFileAttachment -SMObject $SMObject -Path E:\File.csv
    
            #Attach to objects via pipline
            $SMObject | Add-SCSMFileAttachment -Path E:\File.csv
    
            #Attach Files via GUID
            Add-SCSMFileAttachment -ID "3df4b654-f230-bd4a-528a-7c0df4f5f23b" -Path E:\File.csv
    
            #Attach Files in Folder
            Add-SCSMFileAttachment -SMObject $SMObject -Path E:\Folder
    
            #Attache Multiple Files or Folders (not Recusive)
            Add-SCSMFileAttachment -SMObject $SMObject -Path E:\File.csv, E:\Folder, E:\File2.csv
        
        .NOTES
    
        .LINK
    #>
    
    [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Low')]
    param
    (
        <#
        [parameter(
            Mandatory=$false,
            ValueFromPipeline=$false,
            ValueFromPipelibebyPropertyName=$false,
            Position=0,
            ParameterSetName='Set1',
            HelpMessage="Enter Managment Server Computer Name")]
            [Microsoft.EnterpriseManagement.Common.EnterpriseManagementObject]
        [ValidateSet("Value1","Value2")] 
            $Example,       
        #>
        
        [parameter(
            ParameterSetName="Object",
            ValueFromPipeline=$true,
            Mandatory=$true)]
            [Microsoft.EnterpriseManagement.Common.EnterpriseManagementObject]
            $SMObject,
    
        [parameter(
            ParameterSetName="GUID",
            ValueFromPipeline=$true,
            Mandatory=$true)]
            [System.Guid]
            $ID,
    
        [parameter( 
            Mandatory = $true )]
            [string[]]$Path,
    
        [parameter(
            HelpMessage="Enter Managment Server Computer Name")]
            [string]
            $ComputerName
    
    
    )#end param
    BEGIN 
    { 
        #Set SMDefaultComputer
        If ($ComputerName)  
        { 
            $SMDefaultComputer = $ComputerName 
        }
        if(!$SMDefaultComputer)
        {
            Write-Error '$SMDefaultComputer is null in the current session and no -ComputerName parameter was passed to this function. Please specify one or the other.' -ErrorAction Stop
            break
        }
    
    } #End BEGIN
    
    PROCESS
    {
        if ($PSCmdlet.ParameterSetName -eq "GUID")
        {
            $SMObject = Get-SCSMObject -Id $ID
        }
    
        if ($SMObject.ClassName -match "workitem") {
            $RelationshipClassName = "System.WorkItemHasFileAttachment"
            $ProjectionName =        "System.WorkItem.Projection"
            $ObjID =                    $SMObject.ID
            $ActionLogClass =        Get-SCSMClass -Name System.WorkItem.TroubleTicket.ActionLog$
            $ActionLogRel =          Get-SCSMRelationshipClass -Name System.WorkItemHasActionLog$
        } else {
            $RelationshipClassName = "System.ConfigItemHasFileAttachment"
            $ProjectionName =        "System.ConfigItem.Projection"
            $ObjID =                    $SMObject.Id
        }
    
        $ManagementGroup = New-Object Microsoft.EnterpriseManagement.EnterpriseManagementGroup $SMDefaultComputer
        $FileAttachmentRel = Get-SCSMRelationshipClass -Name $RelationshipClassName
        $FileAttachmentClass = Get-SCSMClass -Name System.FileAttachment$
        
    
        #Get the all files
        $AllFiles = $Path |%{Get-ChildItem $_}
    
        #Check how many files were in the directory
        #Also check for any empty files?
    
        Foreach ( $FileObject in $AllFiles ){
            #Create a filestream 
            $FileMode = [System.IO.FileMode]::Open
            $fRead = New-Object System.IO.FileStream $FileObject.FullName, $FileMode
    
            #Create file object to be inserted
            $NewFileAttach = New-Object Microsoft.EnterpriseManagement.Common.CreatableEnterpriseManagementObject($ManagementGroup, $FileAttachmentClass)
            #Populate properties with info
            $SCSMGUID_Attachment = [Guid]::NewGuid().ToString()
            $NewFileAttach.Item($FileAttachmentClass, "Id").Value = $SCSMGUID_Attachment
            $NewFileAttach.Item($FileAttachmentClass, "DisplayName").Value = $FileObject.Name
            $NewFileAttach.Item($FileAttachmentClass, "Description").Value = $FileObject.Name
            $NewFileAttach.Item($FileAttachmentClass, "Extension").Value = $FileObject.Extension
            $NewFileAttach.Item($FileAttachmentClass, "Size").Value = $FileObject.Length
            $NewFileAttach.Item($FileAttachmentClass, "AddedDate").Value = [DateTime]::Now.ToUniversalTime()
            $NewFileAttach.Item($FileAttachmentClass, "Content").Value = $fRead
    
            #Init projection
            $ProjectionType = Get-SCSMTypeProjection -Name $ProjectionName
            $Projection = Get-SCSMObjectProjection -Projection $ProjectionType -Filter "ID -eq $ObjID"
    
    
            #Attach file object to Service Manager
            $Projection.__base.Add($NewFileAttach, $FileAttachmentRel.Target)
            $Projection.__base.Commit()
    
            if ($SMObject.ClassName -match "workitem") {
                $SCSMGUID_ActionLog = [Guid]::NewGuid().ToString()
                $MP = Get-SCSMManagementPack -Name "System.WorkItem.Library"
                $ActionType = "System.WorkItem.ActionLogEnum.FileAttached"
                $NewLog = New-Object Microsoft.EnterpriseManagement.Common.CreatableEnterpriseManagementObject($ManagementGroup, $ActionLogClass)
    
                $NewLog.Item( $ActionLogClass, "Id").Value = $SCSMGUID_ActionLog
                $NewLog.Item( $ActionLogClass, "DisplayName").Value = $SCSMGUID_ActionLog
                $NewLog.Item( $ActionLogClass, "ActionType").Value = $MP.GetEnumerations().GetItem($ActionType)
                $NewLog.Item( $ActionLogClass, "Title").Value = "Attached File"
                $NewLog.Item( $ActionLogClass, "EnteredBy").Value = "SYSTEM"
                $NewLog.Item( $ActionLogClass, "Description").Value = $FileObject.Name
                $NewLog.Item( $ActionLogClass, "EnteredDate").Value = (Get-Date).ToUniversalTime()
    
                #Insert comment to action log
                $Projection.__base.Add($NewLog, $ActionLogRel.Target)
                $Projection.__base.Commit()
            }
    
            #Cleanup
            $fRead.Close();
        }
    
    }#end PROCESS
    
    END
    {  }#end END
    
    }

    Tuesday, October 30, 2018 4:33 PM