Setting Status using PowerShell and converting this into a custom task in Service Manager 2012 SP1

I had a customer who had a need this week to manually force the status of service requests / activities into ‘in progress’ or ‘Completed’.

The most common example I see of this is a need to automatically close service requests / incidents after they have been resolved for x amount of time.

Travis also talks about a common example here where it is sometimes required to reactivate a closed incident.

And one more example is if you have custom status values for work items like Incidents and Service Requests, and need to be able to set the work item to one of those.

As far as the auto-close goes, Cireson currently have a free download that is very popular with a lot of folks to that allows configuration of auto-close workflows through the UI: 

For the other scenarios described above, it is possible to set status is by launching PowerShell and running the following commands:

(if you upgraded from Service Manager 2010)

cd [installation drive]\program files\Microsoft System Center\Service Manager 2010\Powershell

e.g.

cd C:\program files\Microsoft System Center\Service Manager 2010\Powershell

(if you started on Service Manager 2012)

cd [installation drive]\program files\Microsoft System Center 2012\Service Manager\Powershell

e.g.

cd C:\program files\Microsoft System Center 2012\Service Manager\Powershell

From here the commands are the same regardless of your upgrade / install path:

import-module System.Center.Service.Manager.psd1

To change a manual activity to ‘In Progress’:

(get-scsmclass -name "System.WorkItem.activity.ManualActivity" | get-scsmclassinstance) | where {$_.ID -eq 'MA1416'} | %{ $_.Status = "In Progress" ; $_ } | update-scsmclassinstance

To change a manual activity to ‘Completed’:

(get-scsmclass -name "System.WorkItem.activity.ManualActivity" | get-scsmclassinstance) | where {$_.ID -eq 'MA1416'} | %{ $_.Status = "Completed" ; $_ } | update-scsmclassinstance

To change a Service Request to ‘In Progress’:

(get-scsmclass -name "System.WorkItem.ServiceRequest" | get-scsmclassinstance) | where {$_.ID -eq 'SR1415'} | %{ $_.Status = "In Progress" ; $_ } | update-scsmclassinstance

To change a Service Request to ‘Completed’:

(get-scsmclass -name "System.WorkItem.ServiceRequest" | get-scsmclassinstance) | where {$_.ID -eq 'SR1415'} | %{ $_.Status = "Completed" ; $_ } | update-scsmclassinstance

Of course you may want to change other classes in the same way.  In order to do this by tweaking the above examples, you will need the class name which you can get using PowerShell like this:

get-scsmclass –DisplayName “Service Request” | select-object name

get-scsmclass –DisplayName “Review Activity” | select-object name

Note: Don’t let Activities catch you out as it’s not System.WorkItem.ReviewActivity as you may initially guess (I did Smile).  Instead it is System.WorkItem.Activity.ReviewActivity

Alrighty, so you end up with something like this to update a service request from completed back to ‘in progress’:

cd C:\program files\Microsoft System Center 2012\Service Manager\Powershell

import-module System.Center.Service.Manager.psd1

(get-scsmclass -name "System.WorkItem.ServiceRequest" | get-scsmclassinstance) | where {$_.ID -eq 'SR1415'} | %{ $_.Status = "In Progress" ; $_ } | update-scsmclassinstance

Great, so now let’s go one better, and make life a lot easier for the SCSM admin folks by creating a custom task that will allow you to click on a service request and do this.

To do this navigate to Library>Tasks and create the task in the following way:

clip_image001

NOTE: By choosing Service Request, this means when my user has a service request selected in the console, the task will appear in the context-sensitive menu on the right –hand side.  Therefore obviously if you’ve tweaked this for say a manual activity, then you’ll want to be sure to pick the correct class for which you’re command you’re going to use in the next dialog applies to.

Because we are using the Service Request ID as part of our command, I chose not to select any categories, as the class selection above ensures that when a service request is selected, my task will be available.  You may like to use the categories in scenarios where you don’t need to manipulate  a specific instance, for instance with a task that closes service requests that are completed for more than 30 days – you may choose to select the Work Item Folder Tasks category in that scenario.

 

image

 

image

I also chose to log this in the Action log. You don’t really get much in the way of output here as when successful, it just completes without failure, so I chose not to show the output.

The command (for cut and paste purposes) is:

cd ‘C:\Program Files\Microsoft System Center 2012\Service Manager\Powershell’;Import-Module .\System.Center.Service.Manager.psd1;(get-scsmclass -name "System.WorkItem.ServiceRequest" | get-scsmclassinstance) | where {$_.ID -eq '$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Id$'} | %{ $_.Status = 'In Progress' ; $_ } | update-scsmclassinstance

For Manual Activities, you would pick the Manual Activity class instead, and use:

cd ‘C:\Program Files\Microsoft System Center 2012\Service Manager\Powershell’;Import-Module .\System.Center.Service.Manager.psd1;(get-scsmclass -name "System.WorkItem.Activity.ManualActivity" | get-scsmclassinstance) | where {$_.ID -eq '$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Id$'} | %{ $_.Status = 'In Progress' ; $_ } | update-scsmclassinstance

FINAL NOTE: You may not want to make this functionality available to everyone as you don’t want folks breaking all the rules, completing service requests before required activities are completed etc., so through the user roles (Administration >Security), you can narrow down who has access to which task. 

Happy Custom Task-ing Smile