Home CMDB Portal
Options

Batch edit/change Hardware Asset ID and Display Name

Marek_LefekMarek_Lefek Customer Advanced IT Monkey ✭✭✭

Hi, I need to change "Hardware Asset ID" for more than 500 devices. Now "Hardware Asset ID" it is build as ID from previous system and I need to set as serialNumber.

More of this devices has relations to workitems and each other.

Can I change this hardware Id eg by sql or powershell without deletaing the relations?


I also need to change "Display name" and plan to do it by Asset Excel.

These change are needed to set import workflow.

Best Answer

  • Options
    Simon_ZeinhoferSimon_Zeinhofer Customer Ninja IT Monkey ✭✭✭✭
    edited April 2023 Answer ✓
    $SMDefaultComputer = 'YOUR SCSM SERVER'
    
    $hwAssetClass = Get-scsmclass -name 'Cireson.AssetManagement.HardwareAsset$'
    #the old HW Asset
    $oldHWAsset = Get-scsmobject -class $hwAssetClass -filter "HardwareAssetId -eq $oldID"
    
    #Hashtable with all values, key is the name of the property inside the class, and the value comes from the old HW Asset
    $newHWAssetProperties = @{
    "DisplayName" = $newDisplayname #I guess this one comes from your csv?
    "HardwareAssetId" = $newID #I guess you define that in the csv as well?
    "property1" = $oldHWAsset.property1
    "property2" = $oldHWAsset.property2
    .
    .
    .
    .
    }
    
    $newHWAsset = new-scsmobject -class $hwAssetClass -propertyhashtable $newHWAssetProperties -passthru
    
    $sourceRelationships = Get-scsmrelationshipobject -bysource $oldHWAsset | ? {$_.IsDeleted -eq $false}
    $targetRelationships = Get-scsmrelationshipobject -bytarget $oldHWAsset | ? {$_.IsDeleted -eq $false}
    
    foreach($s in $sourceRelationships)
    {
    New-scsmrelationshipobject -relationship (Get-scsmrelationshipclass $s.RelationshipId) -source $newHWAsset -target $s.targetobject -bulk
    }
    
    foreach($t in $targetRelationships)
    {
    New-scsmrelationshipobject -relationship (Get-scsmrelationshipclass $t.RelationshipId) -source $t.sourceobject -target $newHWAsset -bulk
    }
    
    remove-scsmobject -smobject $oldHWAsset -force
    
    
    

    Ofc you would have to add some code by yourself, e.g. how you receive the old HW Asset, how you add all the properties to the hashtable, read the csv, compute the ID...

    But this example should give you a good start

Answers

  • Options
    Simon_ZeinhoferSimon_Zeinhofer Customer Ninja IT Monkey ✭✭✭✭
    edited April 2023

    @Marek_Lefek as the HardwareAssetId is the Key Property for this class, you cannot change it afterwards - Only way to do that would be to create a new asset with all properties from the original one, copy the relationships from one asset to the other and afterwards delete the original asset.

    The displayname can be changed though - I recommend using a csv and then using the

    import-csv..
    

    command - for that you could write the ID inside the first column and the new displayname inside the 2nd one and then just bulk update via first getting the Asset by ID (as this is the key property, only one asset with this ID may exist in your system) and then changing it via

    Set-scsmobject -smobject $asset -property displayname -value $valuefromCSV -passthru
    
    

    If you really want to change the ID it would be more tricky. You would need to create your new asset first, then get all relationship objects from the old asset(for both situations: Hardware Asset is Source or target), then create these relationship objects as a new with your new asset as source/target and as last step delete the old Asset object.

  • Options
    Marek_LefekMarek_Lefek Customer Advanced IT Monkey ✭✭✭

    Thank you @Simon_Zeinhofer , I was affraid about that.

    Does someone can share with some code that can create new HA , reassign the relationships and delete the oldone?

  • Options
    Simon_ZeinhoferSimon_Zeinhofer Customer Ninja IT Monkey ✭✭✭✭
    edited April 2023 Answer ✓
    $SMDefaultComputer = 'YOUR SCSM SERVER'
    
    $hwAssetClass = Get-scsmclass -name 'Cireson.AssetManagement.HardwareAsset$'
    #the old HW Asset
    $oldHWAsset = Get-scsmobject -class $hwAssetClass -filter "HardwareAssetId -eq $oldID"
    
    #Hashtable with all values, key is the name of the property inside the class, and the value comes from the old HW Asset
    $newHWAssetProperties = @{
    "DisplayName" = $newDisplayname #I guess this one comes from your csv?
    "HardwareAssetId" = $newID #I guess you define that in the csv as well?
    "property1" = $oldHWAsset.property1
    "property2" = $oldHWAsset.property2
    .
    .
    .
    .
    }
    
    $newHWAsset = new-scsmobject -class $hwAssetClass -propertyhashtable $newHWAssetProperties -passthru
    
    $sourceRelationships = Get-scsmrelationshipobject -bysource $oldHWAsset | ? {$_.IsDeleted -eq $false}
    $targetRelationships = Get-scsmrelationshipobject -bytarget $oldHWAsset | ? {$_.IsDeleted -eq $false}
    
    foreach($s in $sourceRelationships)
    {
    New-scsmrelationshipobject -relationship (Get-scsmrelationshipclass $s.RelationshipId) -source $newHWAsset -target $s.targetobject -bulk
    }
    
    foreach($t in $targetRelationships)
    {
    New-scsmrelationshipobject -relationship (Get-scsmrelationshipclass $t.RelationshipId) -source $t.sourceobject -target $newHWAsset -bulk
    }
    
    remove-scsmobject -smobject $oldHWAsset -force
    
    
    

    Ofc you would have to add some code by yourself, e.g. how you receive the old HW Asset, how you add all the properties to the hashtable, read the csv, compute the ID...

    But this example should give you a good start

  • Options
    Marek_LefekMarek_Lefek Customer Advanced IT Monkey ✭✭✭

    @Simon_Zeinhofer Thank you for help

Sign In or Register to comment.