Using SMLets to delete a work item

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.

Getting ready

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.

How to do it...

In this particular example, we will take a look at how to delete an incident. To do so, follow these steps:

  1. Log on to the Service Manager management server where you installed the SMLets and start Windows PowerShell ISE.
  2. In the ribbon menu of PowerShell ISE, click Show Script Pane Maximized, and then type the following:
          Import-Module SMLets 
    
  3. Now execute the script by pressing F5 on your keyboard.

    Note

    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.

  4. Press Ctrl + I to get back to the script pane and type the following on a new line:
            $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.

  5. On the next line, type the following:
            $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.

    Tip

    You can use the -Filter parameter for filtering the objects you are retrieving by any property of the class. However, you cannot use the -Filter parameter to filter on several properties at the same time or to filter on relationships.

  6. Replace IR495 with the ID of the incident you want to delete. Then enter the following on a new line and press F5 to execute the script:
    $Incident
  7. The script will now run and display the incident. Before proceeding to the next step, confirm that this is the particular incident that you want to delete. There is no undo button if you accidentally delete the wrong work item!

    How to do it...

  8. Press Ctrl + I to get back to the script pane again and add the following on line 4, right after $Incident (there's a space right before the pipe sign):
             | Remove-SCSMObject -Force 
    
  9. This completes the script and it should now look like the following:
            Import-Module SMLets
            $IncidentClass = Get-SCSMClass System.WorkItem.Incident$
            $Incident = Get-SCSMObject -Class $IncidentClass -Filter "Id -eq IR495"
            $Incident | Remove-SCSMObject -Force
  10. Press F5 to execute the script and delete the incident.

How it works...

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!

Note

Deleting all work items in an environment doesn't mean that the ID counter will get reset. It will still continue to count from the last known value.

There's more...

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:

  • Incident class = System.WorkItem.Incident$
  • Problem class = System.WorkItem.Problem$
  • Change Request class = System.WorkItem.ChangeRequest$
  • Service Request class = System.WorkItem.ServiceRequest$
  • Release Record class = System.WorkItem.ReleaseRecord$

If you want to list all the available classes in Service Manager, you could run Get-SCSMClass without any parameters.

Note

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!

See also

http://smlets.codeplex.com

Please see the recipe Autoclosing resolved Incidents with SMLets and a custom workflow in this chapter for examples of how to use SMLets.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset