SCSM PowerShell: Create Configuration Items

This post is part of a series on Service Manager PowerShell. Be sure to check out the Overview Page for all posts in this series.

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.

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

ci1

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. (https://technet.microsoft.com/en-us/library/Ee692803.aspx)

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.

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.

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.

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.

ci2

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.

ci3

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.

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.

 

Leave a Reply

x

We use cookies to ensure the best possible experience on our website. Detailed information on the use of cookies on this site is provided in our Privacy and Cookie Policy. Further instruction on how to disable our cookies can be found there.