In this recipe, we will take a look at how you can use SMLets to delete a work item. This is something that you cannot perform from the console for several reasons. One of them being that this goes against all the rules of ITIL.
So why would someone need to delete a work item? Well, you might have created a couple of test work items to verify the production environment or to test a workflow, or something similar.
Make sure that you have downloaded and installed SMLets as described in the previous recipe, Downloading and installing SMLets.
You also need administrator rights within Service Manager to be able to delete a work item.
In this particular example, we will take a look at how to delete an incident. To do so, follow these steps:
Import-Module SMLets
The reason for executing this part of the script is to be able to use the PowerShell autocomplete function for SMLets. The autocomplete function allows you to press the Tab key on your keyboard to complete the command syntax. Try it out in the next step! Type Get-SCSMCl
and press the Tab key; PowerShell should now autocomplete the command syntax.
$IncidentClass = Get-SCSMClass System.WorkItem.Incident$
The Get-SCSMClass
cmdlets are used to retrieve a class from Service Manager and as we are going to remove an incident, we need to retrieve the Incident
class.
$Incident = Get-SCSMObject -Class $IncidentClass -Filter "Id -eq IR495"
We are now retrieving a particular incident using the Get-SCSMObject
cmdlets. The Get-SCSMObject
cmdlets can retrieve any object in the Service Manager database. The -Class
parameter tells Get-SCSMObject
that we are looking for an object of the Incident class, and the -Filter
parameter is used to specify which object of the incident class we want to retrieve. In this example, we are looking for an Incident where the ID equals IR495
.
$Incident
$Incident
(there's a space right before the pipe sign):| Remove-SCSMObject -Force
Import-Module SMLets $IncidentClass = Get-SCSMClass System.WorkItem.Incident$ $Incident = Get-SCSMObject -Class $IncidentClass -Filter "Id -eq IR495" $Incident | Remove-SCSMObject -Force
This script effectively removes the particular work item from the database. By defining another filter parameter you can remove several incidents at once and if you skip the filter parameter, you may remove all your incidents from the database. This is both good and bad so think twice before running your scripts in a production environment!
If you would like to delete other type of work items, all you have to do is pass another class to the Get-SCSMObject
command. The name of each class can be tricky to figure out if you are new to Service Manager, and especially to the SMLets, so here is a quick list of the most common ones:
System.WorkItem.Incident$
System.WorkItem.Problem$
System.WorkItem.ChangeRequest$
System.WorkItem.ServiceRequest$
System.WorkItem.ReleaseRecord$
If you want to list all the available classes in Service Manager, you could run Get-SCSMClass
without any parameters.
The dollar sign ($
) at the end of each class name isn't actually a part of the class name itself. It's used as a stop character, telling SMLets to not retrieve anything that starts with the class name. If you don't add the dollar sign, SMLets will actually retrieve subclasses of the class when running the Get-SCSMClass
cmdlets. Try this by running Get-SCSMClass System.WorkItem.Incident
and Get-SCSMClass System.WorkItem.Incident$
and compare the results!
Please see the recipe Autoclosing resolved Incidents with SMLets and a custom workflow in this chapter for examples of how to use SMLets.