SCSM PowerShell: Create Configuration Items | Quisitive
SCSM PowerShell: Create Configuration Items
September 17, 2015
Matthew Dowst
Read below

This post is part of a series on Service Manager PowerShell.

In the previous posts we looked at updating items and relationships using the Service Manager PowerShell cmdlets. Now in this post we are going use the PowerShell cmdlets to actually create new items in Service Manager. We are going to do this using the cmdlet New-SCSMClassInstance. This cmdlet is used to create new Work Items and Configuration Items in Service Manager. In this post we are just going to cover creating Configuration Items. Work Items will be covered in a separate post.

Prior to creating a new object in Service Manager you need to determine what values are available and required for the object type. I have included a script below that will allow you to query a class type and return all of the properties, the property type, and whether or not it is a Key. If the key value of a property is True then the property is required. All other properties are optional.

12345678$class = Get-SCSMClass –name Microsoft.SystemCenter.ConfigurationManager.MobileDevice $properties = @()foreach ($item in @($Class) + @($Class.GetBaseTypes())) {    $properties += $item.PropertyCollection}$properties | sort -Descending Key | FT Name, Type, Key -au

After running the script above for the Mobile Device class you should see an output similar to the one below.

This output tells me that the ID is a required field and all other are optional. Also you’ll notice that AssetStatus and ObjectStatus are enum types. As with updating objects you will want to use the internal name and not the display name for the enums. You can find these using the techniques shown in the post Get List Items.

So now that we have our properties we can use the New-SCSMClassInstance cmdlet to create our configuration items. If you look at the help for the New-SCSMClassInstance cmdlet you will see that the Property parameter is a Hashtable. This means that we will need to include all of the properties we want in a hash table and pass that to the Property parameter.

*PowerShell Tip – Hash Tables

A Hash Table in PowerShell is simply a collection of names and their values. A hash table is a type of array, but instead of storing data using a numeric reference, you are creating unique names and assigning them values. For more information on hash table refer to the TechNet article. (

To create a hash table you can either list each property on a separate line or put them all on one line separating them with a semi-colon. For ease of reading and clarity I will be putting each on a separate line. You start a hash table by using the at symbol followed by curly brackets. The script below show an example of how this could look.

12345678$Properties = @{Id = [string]$([guid]::NewGUID())AssetStatus = “System.ConfigItem.AssetStatusEnum.Deployed”DeviceManufacturer = “Samsung”DeviceModel  = “Note 4″DevicePhoneNumber = “555-555-5555″DeviceImei = “12903810923348230948234098”}

The first line declares the variable Properties then starts the hash table with the @{. Then each following line contains a property name and is value. One thing you may notice is the NewGUID command for the value of the Id property. This is because the Id is a key so it must be a unique value each time. You can make this any value that you want, but to ensure that I will not have a duplicate Id, I’m generating a GUID. The chance of having two GUID with the same value is 1 in 2^128 or 1 in 340,282,366,920,938,463,463,374,607,431,768,211,456.

Service Manager Note
When dealing with Id properties with Work Items you will often see the value set to “{0}”. This tells Service Manager to use the next available number available. For example if the last incident was IR362 and you ran the command to create a new one using “{0}” then the next one would be IR363. We will cover this in more detail in the next post on creating work items.

Now that we have the properties set we can pass it to the New-SCSMClassInstance cmdlet to create our configuration item. To use the New- SCSMClassInstance you just need to provide the class and the properties, as you can see in the example below.

1234567891011$Properties = @{Id = [string]$([guid]::NewGUID())AssetStatus = “System.ConfigItem.AssetStatusEnum.Deployed”DeviceManufacturer = “Samsung”DeviceModel  = “Note 4″DevicePhoneNumber = “555-555-5555″DeviceImei = “12903810923348230948234098”}$Class = get-scsmclass –name ‘Microsoft.SystemCenter.ConfigurationManager.MobileDevice’New-SCSMClassInstance -Class $Class -Property $Properties

When you run the above command it will create the configuration item, and unless there is an error you will not see any output. If you want to see the output you can add the PassThru parameter to the New- SCSMClassInstance command. The PassThru parameter can also be used to save the newly created configuration item to a variable to use later in the script. For example after you create the configuration item you can then create a new relationship instance to set the owner of the item. This is shown in the example below.

12345678910111213141516171819202122$Properties = @{Id = [string]$([guid]::NewGUID())AssetStatus = “System.ConfigItem.AssetStatusEnum.Deployed”DeviceManufacturer = “Samsung”DeviceModel  = “Note 4″DevicePhoneNumber = “555-555-5555″DeviceImei = “12903810923348230948234098”}$Class = get-scsmclass –name ‘Microsoft.SystemCenter.ConfigurationManager.MobileDevice’# Create the config item and save it to the Phone variable$Phone = New-SCSMClassInstance -Class $Class -Property $Properties -PassThru# Get the Owned By User Relationship$OwnerRel = Get-SCSMRelationship -Name System.ConfigItemOwnedByUser# Get the user to set as the owner$Owner = Get-SCSMClassInstance -Class (Get-SCSMClass -Name System.User) | ?{$_.username -eq “TWarren”}# Set the user and the owner of the phoneNew-SCRelationshipInstance -RelationshipClass $OwnerRel -Source $Phone -Target $Owner -PassThru

Bulk Import

You can perform bulk imports utilizing the Import-CSV cmdlet in PowerShell. This cmdlet will let you import the data from a CSV to a custom PowerShell object. Each column in the CSV file becomes a property of the custom object and the items in rows become the property values. In the following example I am going to populate a custom class I created for Microsoft Surfaces. To get started I run the script below get the properties.

12345678$class = Get-SCSMClass –name CAT.Surface$properties = @()foreach ($item in @($Class) + @($Class.GetBaseTypes())) {    $properties += $item.PropertyCollection}$properties | sort -Descending Key | FT Name, Type, Key -au

From here I can create my CSV file that contains the information I want to import. To make things easier on myself I give the columns the same name as the properties in the class.

Now I can import the data to PowerShell using the Import-Csv cmdlet. Then I’ll use a foreach command to parse individually through each line of the CSV, creating the Surface configuration item based on the values of that line, and assigning the owner.

123456789101112131415161718192021222324# Import the CSV file$CSV = Import-Csv “C:Scriptssurfaces.csv”# Parse through each line of the CSVForeach($item in $CSV){    # Create the new Surface CI    $Surface = New-SCSMClassInstance -PassThru (get-scsmclass –name ‘CAT.Surface’) @{        Model = [string]$item.model        OS = [string]$item.os        Capacity = [string]$item.capacity        Memory = [string]$item.memory        PurchaseDate = $item.PurchaseDate        SerialNbr = [string]$item.SerialNbr        CostCenter = [string]$item.CostCenter        }    # Get the user to set as the owner    $Owner = Get-SCSMClassInstance -Class (Get-SCSMClass -Name Microsoft.AD.User) | ?{$_.UserName -eq $item.Owner}        # Set the user and the owner of the Surface    New-SCRelationshipInstance -RelationshipClass (Get-SCSMRelationship -Name System.ConfigItemOwnedByUser) -Source $Surface -Target $Owner -PassThru }

As with any bulk importing automation, I highly recommend only a few items to the CSV to test your script with. Then if everything works go ahead and add the rest.

In the next post I will cover creating Work Items using the PowerShell cmdlets. Also, be sure to check the Overview Post for more content in this series.