Migration of Related Files/Attachments to AssetManagement Items via Powershell

Daniel_PaulDaniel_Paul Customer IT Monkey ✭

Hi Folks,

I spend a good time to find and adjusting a powershell script to download files from Provance ITAM Assets.
I'm now looking for some help to upload the attachments to Cirseon Hardware/Software Assets.

The files are stored on local drive like

c:\Attachments\<unique-HA-key1>\file-1.pdf

c:\Attachments\<unique-HA-key1>\file-2.pdf

c:\Attachments\<unique-HA-key2>\file-1.pdf

Enviroment:
Newest SMLets loaded. (0.5.0.1)
Powershell v5
SCSM 2012 R2 UR9
Provance 2012
Newest Cirseon Asset Managment 2012

But that is for later. I want to upload the files while running the migrations script to have a clean 1:1 assets.

I found one here . But i get following error message when trying with a example file.

PS C:\> Get-Module

ModuleType Name ExportedCommands ---------- ---- ---------------- Script ISE {Get-IseSnippet, Import-IseSnippet, New-IseSnippet} Manifest Microsoft.PowerShell.Management {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...} Manifest Microsoft.PowerShell.Security {ConvertFrom-SecureString, ConvertTo-SecureString, Get-Acl, Get-AuthenticodeSignature...} Manifest Microsoft.PowerShell.Utility {Add-Member, Add-Type, Clear-Variable, Compare-Object...} Manifest Microsoft.WSMan.Management {Connect-WSMan, Disable-WSManCredSSP, Disconnect-WSMan, Enable-WSManCredSSP...} Script SMLets

ErrorMessage:

$CItp.__base.Add($newFileAttach, $FileAttachmentRel.Target)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidOperation: (:) [], RuntimeException
FullyQualifiedErrorId : MethodNotFound

Script:

$managementGroup = new-object Microsoft.EnterpriseManagement.EnterpriseManagementGroup "localhost"
$FileAttachmentRel = Get-SCSMRelationshipClass "System.ConfigItemHasFileAttachment"
$classFA = Get-SCSMClass -name "System.FileAttachment"

$Folder = "C:\temp1"
$File = get-childitem $folder | ?{$_.name -like 'test1.txt'}
$CItp = Get-SCSMObjectProjection System.ConfigItem.Projection -filter "displayname -eq 'ID-BEAMER-0006-08'"

$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()


Best Answer

Answers

  • Daniel_PaulDaniel_Paul Customer IT Monkey ✭
    It worked. After some modifications i got a nice working script. Thanks to you the attachments have been saved :smiley:
    Thanks a lot Roland
Sign In or Register to comment.