Day 78–Using PowerShell to create a Service Request using a Template

Welcome to Day 78 of the “100 Days of DevOps with PowerShell”! For background on our goals in this series, see Announcing the “100 Days of Devops with PowerShell” Series here at SCC.

Back in Day 18 I showed how to create a new incident in Service Manager 2012 using PowerShell. I followed that up with a post on Day 23, showing how to do the exact same thing using the SMLets. I’m going to use the next few posts to show some of the other areas in Service Manager where you might want or need to use PowerShell. In this post, I’m going to discuss how to use PowerShell to create a Service Request in Service Manager, using a template.

Why would you want to do this using PowerShell? Why not just use Orchestrator to programmatically create a Service Request, using the Service Manager activities? What you will find is that you can only create a blank Service Request that way, you can’t create a request and apply a template.  What you can do, however, is use PowerShell in a .NET Script activity to create the request.

This script is going to make use of the SMLets, so you will want to make sure you have them installed on your machine.

Here is the final script:

Let’s break the script into pieces and explain what is happening.

First, I import the smlets module, to make those commands available to me. Next, I get the Service Request class. I will need this class to create my initial service request.  After that, I specify the different list values that I will want to set on my class. A Service Request has several list values on it. In this case, I’m setting the Area, Priority, and Urgency values.  Remember I can use the Get-SCSMEnumeration cmdlet to get all the values of a particular list.  Finally, in this section, I specify the title and description of the service requests.  These are all values that, were I using Orchestrator, I could set using the Orchestrator Databus.

Let’s look at the next snippet of code:

Here, I am creating a hash table containing all the Service Request property values that I want to set (very similar to what I did with Incidents back on Day 18), and then call New-SCSMObject using the Service Request class and my hashtable, to create a new Service Request.  This creates a Service Request in Service Manager with no activities on the activities tab. As a last step in this section, I set the new Service Request ID into a variable for use later in this script. At this point, I now have a new Service Request created in Service Manager, but I’m not finished yet.

Let’s look at the final section of code:

In order to apply a template to the Service Request, I need to get both the Service Request and the template. I start off by getting the Service Request Type Projection.   Type Projections can get a little complicated, but here is a simplified explanation. A Service Request is not just a single class, it is made up of multiple classes linked together by relationships. A Type Projection is a way of “hooking” all those relationships together, so that we can traverse them.  In order to apply my template, I need to get the Service Request type projection, and then use the Get-SCSMObjectProjection to get the service request I created earlier.

Next, I can get the Service Request template that I want to apply, and then I can use Set-SCSMObjectTemplate to apply the template to my service request. Executing the above script will create a service request similar to the following images:



As you can see, we set the different values on the General tab, as well as the activities on the Activities tab, by applying the template.

Leave a Reply

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