Close SR Task CmdLet for Service Manager

I need to preference this by saying that this script is not officially supported. It is solely meant as a demonstrational method to show an example of how we leveraged a SCSM tasks along with integrated PowerShell CmdLets to facilitate one step process of closing Service Requests (SR)

The Scenario:

While testing features of Service Manager 2012 (Portal, Exchange Connector) in our lab, we were generating a large amount of SRs. Ideally; after testing was completed we would close out the Service Requests. Unlike IRs in which you can easily resolved/closed from the Task bar option, Service Requests, by design, are meant to go through certain statuses (new-to- submitted, in progress to complete before they can be closed via the Task bar.

 While this is fine if you are a service desk responding to service requests, we wanted a quick option to close out our test SRs in our lab environment. With that in mind, we decided to create a quick Task bar option (calling a PowerShell script) to clean up any of our test generated SRs without having to go through the proper statuses.

For information on setting up SCSM tasks:

How to Create a Task - https://technet.microsoft.com/library/hh499040.aspx

 For Task Settings:

On the General Tab, we set the target class to Service Request so that it would be an available SR Task, and we specified our lab management pack.

For Command Line we configured it as thus:

The Command line Parameters:

Although we are launching PowerShell in the Full path to command, we wanted to hide the PowerShell command line window, and set the execution policy to allow this script run.

powershell.exe -ExecutionPolicy unrestricted -WindowStyle Hidden –command

Here, we are specifying the $Date variable that will be used to set the closed date on the SR

{$Date = get-date;

If the Service Manager modules are not loaded, the following will import the module from the default Service Manager installation path.

If ( ! (Get-module System.Center.Service.Manager )) { Import-Module 'C:\Program Files\Microsoft System Center 2012\Service Manager\Powershell\System.Center.Service.Manager.psd1'};

The snippet below is getting the SM class instance and service request class, I’m also specifying the SM management server with the ComputerName option.

Get-SCSMClassInstance -class (Get-SCSMClass -ComputerName <SCSMServer> -name system.workitem.servicerequest) - ComputerName <SCSMServer>

Setting the Filter on the selected work item type.

-filter '"Id -eq $Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Id$"'

 Setting the closed date based on the $Date parameter

  %{ $_.ClosedDate = $Date ; $_ }

Adding implementation notes signifying that this SR was closed by a console task.

%{ $_.Notes = '"Closed by console task"'; $_ }

Setting the SR status to Closed

%{ $_.Status='"ServiceRequestStatusEnum.Closed"'; $_ }

And finally, updating the instance.

update-SCClassInstance}

Hopefully this example demonstrates how you can leverage SCSM CmdLets, along with console Tasks to add additional functionality to fit your business needs.

A big thank you goes out to Andrew Barton for assistance with the script..