Script – Automatically Creating a Customer Organisation if it Doesn’t Exist

In a previous blog post I created a PowerShell script that would automatically populate the relationship property for the Customer Organisation class when an Incident is created.

A PowerShell snippet below can be plugged into the original PowerShell script that will automatically create the Customer Organisation if it doesn’t already exist.

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

  {

   # Get the class for Customer Organisation
   $COClass = Get-SCSMClass -Name "CustomerOrganisation"

   # Define the properties that will populate the class instance
   $Properties = @{DisplayName = "$AU.Company"}

   # Create the class instance
   $COCI = New-SCSMClassInstance -Class (Get-SCSMClass -Name $COClass) -property $Properties

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

  }

It uses an If statement, however in the original script there is already an If statement that looks for the CO null value, so if you do plug it in, it may be more efficient to use an Else statement instead.

Happy scripting!

Advertisements

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 )

Google+ photo

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

Connecting to %s