Migration of Related Files/Attachments to AssetManagement Items via Powershell
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 SMLetsErrorMessage:
$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
-
Roland_Kind Partner Advanced IT Monkey ✭✭✭Hi,
I believe the $CItp.__base.x object properties/functions do not exists.
I have modified your Approach replacing the $CItp... Code lines
One additional note:
I have faced some problems using the correct date/time Format - at least it seems to be important that the SCSM Server and your local Pc running the PS-Script should have the same date/time Settings...
Hope this helps you :-)
===
import-Module "C:\Program Files\Microsoft System Center 2012 R2\Service Manager\Powershell\System.Center.Service.Manager.psd1"
get-module "smlets"# this is necessary due to scsm server and ps-script running pc is different
$un = get-credential -credential "domain\username"
$sc_host = "scsmhostname"
$managementGroup = new-object Microsoft.EnterpriseManagement.EnterpriseManagementGroup "scsmhostname"
$FileAttachmentRel = Get-SCSMRelationship -credential $un -computername $sc_host -name "System.ConfigItemHasFileAttachment"
$classFA = Get-SCSMClass -credential $un -computername $sc_host -name "System.FileAttachment"$Folder = "C:\temp"
$File = get-childitem $folder | ?{$_.name -eq 'test1.txt'}# get appropriate workitem where attachement should be added
$wi=get-scsmclassinstance -credential $un -computername $sc_host -name "S007" #workitem$filepath = $Folder +"\" + $File.Name$FileItem = Get-Item -Path $filepath
$fileStream = $FileItem.OpenRead()
$length = $file.Length
$newGuid = [Guid]::NewGuid().ToString();
$AddedDate = (get-date).ToUniversalTime()
# $DateStr=[datetime]"1.10.16 01:04:02"
#
$DateStr=[datetime]-join ($addedDate.Month, "." , $addedDate.day , ".", $addedDate.year , " " , $addedDate.Hour, ":", $addedDate.Minute ,":", $addedDate.Second)$prop = @{
Id = $newGuid
DisplayName=$File.Name
Description=$file.Name
Extension=$file.Extension
Size=$filestream.Length
Content=$filestream
AddedDate=$dateStr
};
#first create the Attachement and after that the relationship - both have to be commited at the same time to the database.
$FileToAdd = new-SCSMObject -credential $un -computername $sc_host -Class $classFA -PropertyHashtable $prop -nocommit
$NewWiRelation = New-SCSMRelationshipObject -credential $un -computername $sc_host -Relationship $FileAttachmentRel -Source $wi -Target $FileToAdd -nocommit
$NewWiRelation.commit()$filestream.close()6
Answers
I believe the $CItp.__base.x object properties/functions do not exists.
I have modified your Approach replacing the $CItp... Code lines
One additional note:
I have faced some problems using the correct date/time Format - at least it seems to be important that the SCSM Server and your local Pc running the PS-Script should have the same date/time Settings...
Hope this helps you :-)
===
import-Module "C:\Program Files\Microsoft System Center 2012 R2\Service Manager\Powershell\System.Center.Service.Manager.psd1"
get-module "smlets"
$un = get-credential -credential "domain\username"
$sc_host = "scsmhostname"
$FileAttachmentRel = Get-SCSMRelationship -credential $un -computername $sc_host -name "System.ConfigItemHasFileAttachment"
$classFA = Get-SCSMClass -credential $un -computername $sc_host -name "System.FileAttachment"
$File = get-childitem $folder | ?{$_.name -eq 'test1.txt'}
$wi=get-scsmclassinstance -credential $un -computername $sc_host -name "S007" #workitem
$fileStream = $FileItem.OpenRead()
$newGuid = [Guid]::NewGuid().ToString();
$AddedDate = (get-date).ToUniversalTime()
# $DateStr=[datetime]"1.10.16 01:04:02"
#
$DateStr=[datetime]-join ($addedDate.Month, "." , $addedDate.day , ".", $addedDate.year , " " , $addedDate.Hour, ":", $addedDate.Minute ,":", $addedDate.Second)
Id = $newGuid
DisplayName=$File.Name
Description=$file.Name
Extension=$file.Extension
Size=$filestream.Length
Content=$filestream
AddedDate=$dateStr
};
#first create the Attachement and after that the relationship - both have to be commited at the same time to the database.
$FileToAdd = new-SCSMObject -credential $un -computername $sc_host -Class $classFA -PropertyHashtable $prop -nocommit
$NewWiRelation = New-SCSMRelationshipObject -credential $un -computername $sc_host -Relationship $FileAttachmentRel -Source $wi -Target $FileToAdd -nocommit
$NewWiRelation.commit()
Thanks a lot Roland