Jump to content

Changing An Asset Class Via PowerShell


Recommended Posts

I wrote a PowerShell script to change An Asset Class from mobileDevice to computer using entityUpdateRecord:

                   

Add-HB-Param        "application" "com.hornbill.servicemanager"
                    Add-HB-Param        "entity" "Asset"
                    Add-HB-Param        "returnModifiedData" "true"
                    Open-HB-Element     "primaryEntityData"
                    Open-HB-Element     "record"
                    Add-HB-Param        "h_pk_asset_id" "506"
                    Add-HB-Param        "h_class" "computer"
                    Add-HB-Param        "h_asset_urn" ("urn:sys:entity:com.hornbill.servicemanager:Asset:")
                    Close-HB-Element    "record"
                    Close-HB-Element    "primaryEntityData"
                    $UpdateAsset = Invoke-HB-XMLMC "data" "entityUpdateRecord"
                    if($UpdateAsset.status -ne 'ok') {
                    Write-Output $UpdateAsset.Error -ErrorAction Stop
                    }
                    else{
                    Write-Output "Asset Primary Record Updated"
                    Add-HB-Param        "application" "com.hornbill.servicemanager"
                    Add-HB-Param        "entity" "Asset"
                    Add-HB-Param        "returnModifiedData" "true"
                    Open-HB-Element     "primaryEntityData"
                    Open-HB-Element     "record"
                    Add-HB-Param        "h_pk_asset_id" "506"
                    Close-HB-Element    "record"
                    Close-HB-Element    "primaryEntityData"
                    Open-HB-Element     "relatedEntityData"
                    Add-HB-Param        "relationshipName" "AssetClass"
                    Add-HB-Param        "entityAction" "update"
                    Open-HB-Element     "record"
                    Add-HB-Param        "h_type" "3"
                    Close-HB-Element    "record"
                    Close-HB-Element    "relatedEntityData"
                    $UpdateAssetRelated = Invoke-HB-XMLMC "data" "entityUpdateRecord"
                    }
                    if($UpdateAssetRelated.status -eq 'ok') {
                    Write-Output "Asset Related Record Updated"
                    }
                    else {
                    Write-Output $UpdateAssetRelated.Error
                    }

If I run it against asset ID 506, I get:

Asset Primary Record Updated
There are no values to update

You can see from the attached image that the record does actually change the class to computer an the type to physical as expected:

If I use any other asset ID, I get:

A database or query error occurred while updating record(s)

Any idea what's special about 506 and why it works for that record. but will not work for any others?

Capture.PNG

Link to comment
Share on other sites

Hi @Rob Gething,

It appears that you are changing the Asset Class. Each Asset Class is linked to a particular secondary table.

Your API actions have likely broken the link between the main CMDB record and the secondary table data.

I suspect you tested a few things with Asset 506 which changed that link in a different way to the assets you are currently testing. Hence the difference in behaviour/error message.

IT is currently not possible via the GUI to change the Asset Class of an asset and there is currently no API to change the Asset Class of an asset either.

The asset class data link mentioned might not be the only link maintained behind-the-scenes. It is ill-advised to directly manipulate the data without knowing/imagining the possible consequences.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...