I do currently a lot of automation and today I had a need to change state of a service request. The service request itself was “Closed” and as you might know, you cannot change anything if the work item is set to “Closed”. Because I needed to trigger the runbook activities behind the service request again, I needed to “reactivate” the service request. How are we doing this? Of course, using PowerShell and SMLets.
The service request looked like this and nothing can be changed anymore…
So I created this script to change the state of work items and activities.
Save this script as Change-WorkItemState.ps1
Param ( [Parameter(Mandatory=$true)][String]$ObjectID, [Parameter(Mandatory=$true)]` [ValidateSet("Active","Cancelled","Completed","Failed",` "OnHold","Ready","Rerun","Skipped","Closed","Active.Pending",` "Resolved","New","Failed","InProgress","Submitted","Editing")][String]$Status ) try { # Settings and modules ##################################################################### $ErrorActionPreference = "Stop" $Error.Clear() If (!(Get-Module SMLets)) {Import-Module SMLets -Force} ##################################################################### # Classes and enumerations ##################################################################### #Get the first two characters of the $ObjectID e.g. IR1234 = IR and then get the class and enumeration Switch (($ObjectID.Substring(0,2))) { "MA" {$Class = Get-SCSMClass -Name ^System.WorkItem.Activity$;$EnumStatus = Get-SCSMEnumeration -Name ^ActivityStatusEnum.$Status$} "RB" {$Class = Get-SCSMClass -Name ^System.WorkItem.Activity$;$EnumStatus = Get-SCSMEnumeration -Name ^ActivityStatusEnum.$Status$} "SA" {$Class = Get-SCSMClass -Name ^System.WorkItem.Activity$;$EnumStatus = Get-SCSMEnumeration -Name ^ActivityStatusEnum.$Status$} "PA" {$Class = Get-SCSMClass -Name ^System.WorkItem.Activity$;$EnumStatus = Get-SCSMEnumeration -Name ^ActivityStatusEnum.$Status$} "SR" {$Class = Get-SCSMClass -Name ^System.WorkItem.ServiceRequest$;$EnumStatus = Get-SCSMEnumeration -Name ^ServiceRequestStatusEnum.$Status$} "PR" {$Class = Get-SCSMClass -Name ^System.WorkItem.Problem$;$EnumStatus = Get-SCSMEnumeration -Name ^ProblemStatusEnum.$Status$} "RR" {$Class = Get-SCSMClass -Name ^System.WorkItem.ReleaseRecord$;$EnumStatus = Get-SCSMEnumeration -Name ^ReleaseStatusEnum.$Status$} "IR" {$Class = Get-SCSMClass -Name ^System.WorkItem.Incident$;$EnumStatus = Get-SCSMEnumeration -Name ^IncidentStatusEnum.$Status$} "CR" {$Class = Get-SCSMClass -Name ^System.WorkItem.ChangeRequest$;$EnumStatus = Get-SCSMEnumeration -Name ^ChangeStatusEnum.$Status$} } ################################################################### #Find the object $Object = Get-SCSMObject -Class $Class -Filter "Name -eq $ObjectID" #Set object state and write output information If($Object) { Write-Host "Changing Status of $Object to $EnumStatus" -ForegroundColor Cyan Set-SCSMObject -SMObject $Object -Property Status -Value $EnumStatus Start-Sleep -Seconds 2 Write-Host "Status of $Object changed to $EnumStatus" -ForegroundColor Green } Else { Write-Host "$ObjectID not found" -ForegroundColor Red } } catch { Throw $Error[0].Exception } finally { If (Get-Module SMLets) {Remove-Module SMLets -Force} }
You need to provide the ID of the object like IR1234, SR4978, RB3214 etc. for which you want to change state. For the different work item and activities you can set the following states ( I hope I got all)…
Servicer Request (SR)
- New
- Closed
- Completed
- Failed
- Cancelled
- On Hold
- In Progress
- Submitted
Problem Record (PR)
- Active
- Resolved
- Closed
Release Record (RR)
- New
- Cancelled
- On Hold
- Failed
- In Progress
- Completed
- Closed
Incident Record (IR)
- Active
- Closed
- Active.Pending (Pending)
- Resolved
Change Request (CR)
- New
- Failed
- In Progress
- On Hold
- Cancelled
- Submitted
- Completed
Manual Activity (MA), Runbook Activity (RB), Parallel Activity (PA), Sequential Activity (SA)
- Completed
- Cancelled
- Skipped
- In Progress
- On Hold
- Failed
- Rerun
- Pending
Now let’s run an example, let’s assume you have an incident IR5754 and the state is “Resolved” and you want to change it to “Pending” …
Run the script…
et voilà….
The same procedure works for all activities like Manual Activity (MA), Runbook Activity (RB), Parallel Activity (PA), Sequential Activity (SA) and all the work items listed above. Make sure you have the SMLets installed on the computer you are running this script.
Conclusion: You can change state of any work item or activity easily using PowerShell. It is even possible to “re-animate” closed work items like a service request.
Have fun!
Terrific ! Had a bunch of service request not willing to complete. This script did the job !