How reactivate a completed Service Request when a new Activity is added
Recently I was asked how to make a service request change its status from ‘Completed’ to ‘In Progress’ when a new activity was added as a last one in Microsoft System Center 2012 Service Manager. This customer said that it was required in some scenarios and told that it worked for change requests. The idea was to create a custom workflow to realize the required logics.
So I did. It is a quick solution based on SMlets cmdlets.
Here is PowerShell code I used:
$m = (get-module|%{$_.name}) -join " "
if(!$m.Contains("SMLets")){Import-Module SMLets -ErrorVariable err -Force}
# -------------------get class and object------------------------
$WI_A_rs = Get-SCSMRelationshipClass System.WorkItemContainsActivity
$Completed4SR = Get-SCSMEnumeration -Name ServiceRequestStatusEnum.Completed
$InProgress4SR = Get-SCSMEnumeration -Name ServiceRequestStatusEnum.InProgress
$Completed4A = Get-SCSMEnumeration -Name ActivityStatusEnum.Completed
$Active4A = Get-SCSMEnumeration -Name ActivityStatusEnum.Active
$Ready4A = Get-SCSMEnumeration -Name ActivityStatusEnum.Ready
$A = Get-SCSMObject -Id ([guid]$A_GUID).ToString()
$WI = Get-SCSMObject -Id ((Get-SCSMRelationshipObject -ByTarget $A | ?{$_.RelationshipID -eq $WI_A_rs.Id.ToString()}).SourceObject.Id.Tostring())
# --------------------Check and set statuses--------------------------
if ($WI.Status -eq $Completed4SR -and $A.SequenceId -gt 0)
{
$As = Get-SCSMRelatedObject -SMobject $WI -Relationship $WI_A_rs
$PreA = $As|?{$_.SequenceId -eq ($A.SequenceId -1)}
if ($PreA.Status -eq $Completed4A -and $A.Status -eq $Ready4A -and ($As.count -1) -eq $A.SequenceId)
{
Set-SCSMObject -SMObject $A -Property Status -Value $Active4A
Set-SCSMObject -SMObject $WI -Property Status -Value $InProgress4SR
}
}
remove-module -name SMLets –force
As you can see the script is quite short and simple. Using Service Manager Authoring Tool I created a management pack that included the script’s code as well as dll file that you have put in Service Manager’s folder in Program Files.
I have attached the archive with all source files to this post. Maybe later I will publish in the TechNet gallery.
Have a nice day, dear reader!