Home Asset Management

PowerShell - Change Cost Center of Cireson Hardware Asset

Robin_FaklerRobin_Fakler Customer IT Monkey ✭
edited January 2021 in Asset Management

Hello everybody,

I want to change the cost center of Cireson Hardware Assets but I can´t get it to work :(

Here is my script:

###################
# START OF SCRIPT #

###################




# Get the needed SCSM classes

$ClassHWA = Get-SCSMClass -Name "Cireson.AssetManagement.HardwareAsset"

$RelClassHWAPU = Get-SCSMRelationshipClass -Name "Cireson.AssetManagement.HardwareAssetHasPrimaryUser"

$RelClassCostCenter = Get-SCSMRelationshipClass -Name "Cireson.AssetManagement.HardwareAssetHasCostCenter"




# Get SCSM hardware asset

$HWA = Get-SCSMObject -Class $ClassHWA | Where-Object {$_.DisplayName -like "*LBCNBT110*"}




# Get the cost center relationship object of the hardware asset

$GetRelAsset_CostCenter = Get-SCSMRelationshipObject -Relationship $RelClassCostCenter | Where-Object {$_.SourceObject -like "*$($HWA.DisplayName)*"}




$GetRelAsset_CostCenter.TargetObject # Output: ClassName = Cireson.AssetManegement.CostCenter / DisplayName = 1BA / LastModified ...

$GetRelAsset_CostCenter.TargetObject.DisplayName # Output: 1BA




# Get the projection type

$ProjType = Get-SCSMTypeProjection -name "Cireson.AssetManagement.HardwareAsset.ProjectionType" 

#$ProjType = Get-SCSMTypeProjection -name "Cireson.AssetManager.CostCenter"




# Get the projection object

$GetProjTypeAsset = Get-SCSMObjectProjection -ProjectionObject $ProjType -Filter "Name -eq LBCNBT110"




$GetProjTypeAsset.Target_HardwareAssetHasCostCenter # Cireson.AssetManegement.CostCenter / DisplayName = 1BA / LastModified ...

$GetProjTypeAsset.Target_HardwareAssetHasCostCenter.DisplayName # Output: 1BA




# Prepare new cost center value in hash table

$HashProperties = @{"Target_HardwareAssetHasCostCenter" = "6NA"} # New value




# Set object projection with new cost center value

Set-SCSMObjectProjection -Projection $GetProjTypeAsset -PropertyValues $HashProperties

#################

# END OF SCRIPT #

#################


I get following error messages:


Error Message 1 (PowerShell ISE):

Set-SCSMObjectProjection : Object reference not set to an instance of an object.
At line:3 char:1

+ Set-SCSMObjectProjection -Projection $GetProjTypeAsset.Target_Hardwar ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  + CategoryInfo     : NotSpecified: (:) [Set-SCSMObjectProjection], NullReferenceException

  + FullyQualifiedErrorId : System.NullReferenceException,SMLets.SetSCSMObjectProjectionCommand


Error Message 2 (Visual Studio Code):

Set-SCSMObjectProjection : Target_HardwareAssetHasCostCenter
At line:1 char:1

+ Set-SCSMObjectProjection -Projection $GetProjTypeAsset -PropertyValue ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  + CategoryInfo     : NotSpecified: (LBCNBT110:EnterpriseManagementObject) [Set-SCSMObjectProjection], ObjectNotFoundException

  + FullyQualifiedErrorId : property not found on object,SMLets.SetSCSMObjectProjectionCommand

I can read the cost center from the projection but I can´t edit it:

I hoped editing is so easy as removing the value of the cost center :P

Remove-SCSMRelationshipObject -SMObject $GetRelAsset_CostCenter


I have the feeling that I´m just one line of code away from success but sadly I don´t get it.


Do you have any ideas? :)

Ty!

Best Answer

  • Geoff_RossGeoff_Ross Cireson Consultant O.G.
    Answer ✓

    Hi Robin,

    Just been playing with this myself and I'm not actually sure its possible like that. You might have to forget the projection and just update the relationships directly.

    Looking at your code, this may have been how you started as you already gather all the relationships and classes.

    ###################
    
    # START OF SCRIPT #
    
    ###################
    
    
    # Get the needed SCSM classes
    
    $ClassHWA = Get-SCSMClass -Name "Cireson.AssetManagement.HardwareAsset"
    
    $ClassCC = Get-SCSMClass -Name "Cireson.AssetManagement.CostCenter"
    
    $RelClassHWAPU = Get-SCSMRelationshipClass -Name "Cireson.AssetManagement.HardwareAssetHasPrimaryUser"
    
    $RelClassCostCenter = Get-SCSMRelationshipClass -Name "Cireson.AssetManagement.HardwareAssetHasCostCenter"
    
    
    # Get SCSM hardware asset
    
    $HWA = Get-SCSMObject -Class $ClassHWA | Where-Object {$_.DisplayName -like "*LBCNBT110*"}
    
    
    # Get the objects to relate
    
    $CostCenter = Get-SCSMObject -Class $ClassCC | Where-Object {$_.DisplayName -like "*6NA*"}
    
    
    # Set the new relationships
    
    New-SCSMRelationshipObject -Relationship $RelClassCostCenter -Source $HWA -Target $CostCenter -Bulk
    
    #################
    
    # END OF SCRIPT #
    
    #################
    

Answers

  • Geoff_RossGeoff_Ross Cireson Consultant O.G.

    Hi @Robin_Fakler

    You are pretty close I think.

    The issue is this line:

    $HashProperties = @{"Target_HardwareAssetHasCostCenter" = "6NA"} # New value
    

    You need to give it an object (of type CostCenter) not just the string.

    You need something like this:

    $ClassCC = Get-SCSMClass -Name "Cireson.AssetManagement.CostCenter"
    
    $CostCenter = Get-SCSMObject -Class $ClassCC | Where-Object {$_.DisplayName -like "*6NA*"}
    
    $HashProperties = @{"Target_HardwareAssetHasCostCenter" = $CostCenter} # New value
    

    This gets the object and then sets that in the projection.

    FYI, I know you've just been experimenting but this code is not the most efficient way of doing things. For one, you get a lot a data into variables that you never use. Secondly, the Get-SCSMObject | Where-Object is much slower than Get-SCSMObject -Filter

    I've followed your method here to help you understand but if you want some bedtime reading...

    Geoff

  • Robin_FaklerRobin_Fakler Customer IT Monkey ✭

    Hi Geoff,

    thank you very much for your input. I tried your code above but unfortunately I get an other error:

    It looks like that the property "Target_HardwareAssetHasCostCenter" can not be found in the used projection type "Cireson.AssetManagement.HardwareAsset.ProjectionType".

    $ProjType = Get-SCSMTypeProjection -Name "Cireson.AssetManagement.HardwareAsset.ProjectionType"  
    

    But this property exists like you see in the screenshot of my first post?!


    Thanks for the link! :)

  • Geoff_RossGeoff_Ross Cireson Consultant O.G.

    Robin,

    Try just "CostCenter" is the key in that hashtable.

    $HashProperties = @{"CostCenter " = $CostCenter} # New value
    

    Geoff

  • Robin_FaklerRobin_Fakler Customer IT Monkey ✭

    Hi Geoff,

    same error :( I tried it also with "Cireson.Asset.Management.CostCenter".


  • Geoff_RossGeoff_Ross Cireson Consultant O.G.
    Answer ✓

    Hi Robin,

    Just been playing with this myself and I'm not actually sure its possible like that. You might have to forget the projection and just update the relationships directly.

    Looking at your code, this may have been how you started as you already gather all the relationships and classes.

    ###################
    
    # START OF SCRIPT #
    
    ###################
    
    
    # Get the needed SCSM classes
    
    $ClassHWA = Get-SCSMClass -Name "Cireson.AssetManagement.HardwareAsset"
    
    $ClassCC = Get-SCSMClass -Name "Cireson.AssetManagement.CostCenter"
    
    $RelClassHWAPU = Get-SCSMRelationshipClass -Name "Cireson.AssetManagement.HardwareAssetHasPrimaryUser"
    
    $RelClassCostCenter = Get-SCSMRelationshipClass -Name "Cireson.AssetManagement.HardwareAssetHasCostCenter"
    
    
    # Get SCSM hardware asset
    
    $HWA = Get-SCSMObject -Class $ClassHWA | Where-Object {$_.DisplayName -like "*LBCNBT110*"}
    
    
    # Get the objects to relate
    
    $CostCenter = Get-SCSMObject -Class $ClassCC | Where-Object {$_.DisplayName -like "*6NA*"}
    
    
    # Set the new relationships
    
    New-SCSMRelationshipObject -Relationship $RelClassCostCenter -Source $HWA -Target $CostCenter -Bulk
    
    #################
    
    # END OF SCRIPT #
    
    #################
    
  • Robin_FaklerRobin_Fakler Customer IT Monkey ✭

    Hi Geoff,

    thank you (again) for solving my problems! :) That was way easier then I have expected because you need the relationship between the asset and the cost center to read the value of the current set cost center. But you can change and delete it without the relationship reference.

    Thank you again for the link how to query SCSM, my scripts are working waaaay faster :)

  • Robin_FaklerRobin_Fakler Customer IT Monkey ✭
    edited January 2021

    @Geoff_Ross


    Is there a faster way to query SCSM Relationship Objects like in the example below?

    $RelClassHWAPU = Get-SCSMRelationshipClass -Name "Cireson.AssetManagement.HardwareAssetHasPrimaryUser"
    $GetRelAsset_PrimaryUser = Get-SCSMRelationshipObject -Relationship $RelClassHWAPU | Where-Object {$_.SourceObject -eq "ASSETNAME"}
    


    I have tried to adapt it like in your posted article but I can´t get it to work because of an invalid property:

    $GetRelAsset_PrimaryUser = Get-SCSMRelationshipObject -Relationship $RelClassHWAPU -Filter "SourceObject -eq 'ASSETNAME'"
    # Error:
    # Get-SCSMRelationshipObject : The property SourceObject is not valid for the given criteria.
    
    


  • Justin_WorkmanJustin_Workman Cireson Support Super IT Monkey ✭✭✭✭✭

    @Robin_Fakler - On the last question, I usually go the other way.

    $GetRelAsset_PrimaryUser = (Get-SCSMRelationshipObject -BySource $HWA | Where-Object {$_.RelationshipId -eq $RelClassHWAPU.Id}).TargetObject

  • Geoff_RossGeoff_Ross Cireson Consultant O.G.

    @Robin_Fakler

    +1 to Justin's work above. I have not ever managed to get the -Filter param to work so pick the param I think will produce the least results out of -Relationship, -BySource and -ByTarget and then use Where-Object to filter the results by the other items.

    Geoff

  • Robin_FaklerRobin_Fakler Customer IT Monkey ✭

    Hi @Justin_Workman @Geoff_Ross,

    thank you, your code example is like 10x faster then my code above! :)

Sign In or Register to comment.