Automatically delete Objects that have the Status “Pending Delete”

When Configuration Items are deleted from the CMDB, these objects are not deleted instantly. Instead they are marked as deleted and are removed from the CI views. These CIs are now in the status “Pending Delete” and will remain in this state. Such Objects are visible in the folder “Deleted Items” under the Administration wunderbar. When objects are marked in this folder, you have the possibility to restore them back to a regular state or delete them permanently. Permanently means, that the status again changes, this time to “Deleted”. And those (and only those) objects are removed by the purging workflow from the CMDB (Important: They will not be deleted from the Data Warehouse!). This blog post shows how you can bulk delete objects that are in the “Pending Delete” state by using a simple Powershell script.

This Script can be used to automate the permanently deletion of objects. Otherwise objects stay in the state “Pending Delete” until you remove them manually. You can run the script by using a Service Manager workflow, Opalis or a simple scheduled task. Here we go with the script.

import-smlets
$class = Get-SCSMClass | where{$_.name -match “system.configitem”}
$deletedobjects = Get-SCSMObject -class $class | where{$_.objectstatus -match “Pending”}
$deletedobjects | set-scsmobject -property objectstatus -value deleted

The first line is used to import the smlets, they are needed and you can get them here. The second line is used to get objects from the Configuration Item class later. The third line gets objects from the Configuration Item class that have the status “Pending Delete”. And the last line updates the status of all those objects to “Deleted”.

For detailled information about the grooming process in the Service Manager database, see this article.

regards
Marcel

This entry was posted in SCSM and tagged , , . Bookmark the permalink.

6 Responses to Automatically delete Objects that have the Status “Pending Delete”

  1. Guido says:

    Hi Marcel, Can i keep on the CMDB all CIs that were deleted from AD or SCCM?? Regards.

  2. Jim says:

    Is there any way the script can be tweaked to just remove deleted users only, not CI’s?

    • Hey

      Sure, just change the class. Here’s an example with a slightly different code than in the post:

      import-module smlets
      $class = get-scsmclass microsoft.windows.computer$ #### Change to the class id you need
      $enum = (Get-SCSMEnumeration System.ConfigItem.ObjectStatusEnum.PendingDelete$).id
      $deletedobjects = Get-SCSMObject -class $class -filter “ObjectStatus -eq ‘$enum'”
      $deletedobjects | set-scsmobject -property objectstatus -value deleted

      Cheers
      Marcel

  3. Baltazar says:

    Nice tip! 🙂

    Is there a way to restore a list of CIs with pending delete using the script?

    Not all of them, just a list.

    Thank!

  4. Andre Prins says:

    Thanks,
    for 2012 R2 the script needs a bit of tweaking, but your script gave the right direction, so it was not too hard :

    $class = Get-SCSMClass | where{$_.name -match “system.configitem”}
    $deletedobjects = Get-SCSMClassInstance -class $class| where {$_.objectstatus -match “pending”}
    $deletedobjects | %{ $_.ObjectStatus = ”deleted” ; $_ } | update-SCClassinstance

    Regards,
    Andre

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s