Definitive Service Manager Powershell Part 2
Following up on our definitive commands from the previous post, let’s talk about using a couple of them in practice. For these examples, I’m going to be using SMLets. Prefer to see this with stock commands? Let me know and I can come back with them in a later post.
Get an Incident, Service Request, or Anything Else
We’re going to start super simple with a couple read-only based operations. Retrieving one Incident (a subclass/type of Work Item) out of Service Manager and displaying it in PowerShell. This requires only 2 of our definitive commands and just 2 lines of PowerShell.
$irClass = Get-SCSMClass –name "System.WorkItem.Incident$"
Get-SCSMObject –class $irClass –filter "Name -eq 'IR1234'"
Tip: It’s important to note I’m using the –filter parameter to ensure I grab the absolute least amount of data possible. This statement also holds true of PowerShell commands even outside of Service Manager! So in the case of SCSM, if you were to drop the –filter parameter entirely you would return EVERY object of that class. Just the kind of thing you would not want to do during the middle of the day as it’s potentially a wealth of data coming across the wire to your workstation. Make sure when you’re retrieving data from Service Manager, you are going after a particular “shape” of Incident. Since I’ll be using the term often, when I say shape I mean a particular subset of items (or Objects) of a particular Class. For example, Incidents (a Class) whose Title is like X or in our case – the Incident who goes by IR1234. The Filter parameter does exactly what its name implies!
As should be the case, you won’t be logged onto an SCSM management server to run commands. In order to run the above remotely from your local computer against your Service Manager infrastructure requires just a little bit of adaptation.
$irClass = Get-SCSMClass –name "System.WorkItem.Incident$" -computername "mgmtServer01"
Get-SCSMObject –class $irClass –filter "Name -eq 'IR1234'" -computername "mgmtServer01"
Where “mgmtServer01” is the name of a management server or load balanced name in your environment. But you’ll notice something, we’re not exactly seeing all the properties on the Incident. PowerShell returned at best a summary of the Incident. What if we want to see everything IR1234 has to offer?
$irClass = Get-SCSMClass –name "System.WorkItem.Incident$" -computername "mgmtServer01"
Get-SCSMObject –class $irClass –filter "Name -eq 'IR1234'" -computername "mgmtServer01" | select-object *
By using the pipeline operator in PowerShell we can use the Incident we retrieved in Service Manager, as the input to the command on the right of the pipeline. In this case, a select-object * which shows us all the properties on the returned Incident. The best part of the above is that we can easily adapt it to get next to anything else in Service Manager. Let’s try to get a Service Request (another subclass/type of Work Item) next.
$srClass = Get-SCSMClass –name "System.WorkItem.ServiceRequest$" -computername "mgmtServer01"
Get-SCSMObject –class $srClass –filter "Name -eq 'SR1235'" -computername "mgmtServer01" | select-object *
If you look closely, you’ll notice very little has changed between our Incident retrieval and Service Request retrieval. We updated the variable name and the name of the Class we were working with. So next let’s try to get something that is not a Work Item. Let’s get a Hardware Asset (a subclass/type of Configuration Item) out of Service Manager.
$catalogItemClass = Get-SCSMClass –name "Cireson.AssetManagement.CatalogItem$" -computername "mgmtServer01"
Get-SCSMObject –class $ catalogItemClass –filter "DisplayName -eq 'Standard Laptop'" -computername "mgmtServer01" | select-object *
Seeing a pattern here? In just two lines of PowerShell we’re able to retrieve whatever we want out of Service Manager. We first declare the Class we want to work with then get an Object from that Class and filter down. Whether the object was created from a connector, Asset Excel, your analysts, or your Asset Managers you can get what you need in seconds with PowerShell. Last but not least a question that is absolutely worth addressing –
How do you know what class names are?
A perfectly fair question when you’re starting to experiment in SCSM based PowerShell. If we wind the clock back to when I was just getting started with PowerShell and Service Manager my first guess was:
Get-SCSMClass –name "incident" -computername "mgmtServer01"
And I would absolutely encourage you to run the above command. Because when it comes to SMLets, Get-SCSMClass actually runs with a built-in wildcard. Whatever you put after the –name parameter, the command searches throughout all of SCSM looking for where that word occurs in the name of the Class. With the above results returned I’m looking at the “DisplayName” column for what feels like makes the most sense. Then I place a $ at the end of the true class name once I’ve found it. In doing so, I’m ensuring I retrieve 1 Class and 1 Class alone. If I didn’t do this, my next Get-SCSMObject command would fail as the –class parameter is expecting a single Class. In SMLets, the $ is the way to stop or prevent the wildcard search from happening further.
But even if I picked the wrong Class here, I know that I’m wrong relatively quickly because if I attempted a Get-SCSMObject and filtered looking for an Incident that I know for a fact exists (e.g. I had it open in the Cireson SCSM Portal) and returned no results – I’d know instantly I picked the wrong Class.
Tip: If you pick the wrong class, don’t view it as a mistake. You now know what not to pick next time! This guessing in PowerShell is a natural part of the learning process that everyone goes through.
With that said, it’s about time to wrap up part 2. Do you need to know every single class name in Service Manager? No. Will you come to understand a pattern of how they are named and be able to guess them going forward? Very likely. To get you started, here are some Work Item and Configuration Item class names. But where are the rest like Manual Activities? Problems? or Hardware Assets you ask? They can be found in…
Homework Assignment #1
Try and retrieve Objects out of your environment from different Classes like Change Request, Review Activity, or Hardware/Software Assets.
- First, just retrieve a single item/object
- Second, try and retrieve several or a particular “shape” of items
- If you’re looking for bonus points – do either of the above in a single, 1 line of PowerShell
$irClass = Get-SCSMClass -name "System.WorkItem.Incident$"
$srClass = Get-SCSMClass -name "System.WorkItem.ServiceRequest$"
$paClass = Get-SCSMClass -name "System.WorkItem.Activity.ParallelActivity$"
$catalogItemClass = Get-SCSMClass –name "Cireson.AssetManagement.CatalogItem$"
$consumableClass = Get-SCSMClass –name "Cireson.AssetManagement.Consumable$"
I’ve started a community thread specifically for this blog series over here. Post your questions, feedback, or your answers. Because when you share everyone learns. See everyone next week for Part 3!
Missed Part 1? – Catch Up Here!