PowerShell – Dynamically Adding the Cireson Asset Management Organisation to a Work Item

I wrote a post a little while back that demonstrates how to automatically add the relevant customer to an Incident.  It assumes I created a new class called Customer Organisation.

However, if you are using the Cireson Asset Management app then you could use the Organisation class that comes with this app.

This assumes you author a relationship between the Organisation class and the Incident Class.

I use the same command to store the relationship as a variable.

#Store the relationship types as a variables
$CORel = Get-SCSMRelationship -Name "RelatedCustomerOrganisation"

Here is the key difference in the script – specifying the Cireson AM Organisation.

# Obtain the Customer Organisation where the name matches the users Company property
$CO = Get-SCSMClassInstance -Class (Get-SCSMClass -Name Cireson.AssetManagement.Organization) | ? {$_.DisplayName -eq $AU.Company}

The full script is here.

Import-Module 'C:\Program Files\Microsoft System Center 2012 R2\Service Manager\Powershell\System.Center.Service.Manager.psd1'

#Store the relationship types as a variables
$CORel = Get-SCSMRelationship -Name "RelatedCustomerOrganisation"
$AURel = Get-SCSMRelationship -Name "System.WorkItemAffectedUser"

# Get the Incident
$IR = Get-SCSMClassInstance -Class (Get-SCSMClass -Name System.WorkItem.Incident) | ? {$_.ID -eq "$GUID"}

# Use the method to return the related Affected User object GUID and save it to a variable
$AffectedUserGUID = $IR.GetRelatedObjectsWhereSource($AURel.ID).EnterpriseManagementObject.id.GUID

# If related Affected User is returned then process the remainder of the script
if ($AffectedUserGUID -ne $null)

{

# Obtain CI info about the Affected User
$AU = Get-SCSMClassInstance -ID $AffectedUserGUID

# Obtain the Customer Organisation where the name matches the users Company property
$CO = Get-SCSMClassInstance -Class (Get-SCSMClass -Name Cireson.AssetManagement.Organization) | ? {$_.DisplayName -eq $AU.Company}

# If a match between the user and the Customer Organisation is found then create the relationship
if ($CO -ne $null)

{

#Create the relationship between the Incident and the Customer Organisation CI
New-SCRelationshipInstance -RelationshipClass $COREL -Source $CO -Target $IR

}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.