Custom Workflow issue with SR Completed Date

I needed to create a workflow that will cancel pending Activities for Failed and Cancelled SR. I found Travis's CR technet and was able to reproduce it for SR. I also needed to include the Completed Date value in order to get the SLA to stop on a cancelled and failed SR.

Here is my script:

$InProgress = Get-SCSMEnumeration -Name ActivityStatusEnum.Ready
$Failed = Get-SCSMEnumeration -Name ActivityStatusEnum.Failed
$Cancelled = Get-SCSMEnumeration -Name ActivityStatusEnum.Cancelled
$SRCancelled = Get-SCSMEnumeration ServiceRequestStatusEnum$
$SRStatusCancelled = Get-SCSMChildEnumeration -Enumeration $SRCancelled |?{$_.DisplayName -eq "Cancelled"}
$Date = Get-Date -Format g
$SRequest = Get-SCSMObject -Class (Get-SCSMClass -Name System.WorkItem.ServiceRequest$) -Filter "Id -eq $ServiceRequestID"
$SRImpList = Get-SCSMEnumeration ServiceRequestImplementationResultsEnum$
$SRImpStatusFailed = Get-SCSMChildEnumeration -Enumeration $SRImpList |?{$_.DisplayName -eq "Failed"}
$SRImpStatusCancelled = Get-SCSMChildEnumeration -Enumeration $SRImpList |?{$_.DisplayName -eq "Cancelled"}
$ChildActivities = (Get-SCSMRelationshipObject -BySource $SRequest | ?{$_.RelationshipID -eq "2da498be-0485-b2b2-d520-6ebd1698e61b"})

$SRequest| Set-SCSMObject -Property CompletedDate -Value $Date
$ChildActivities | %{$Activity = Get-SCSMObject -ID $_.TargetObject.Id; if($Activity.Status -eq $InProgress){$Activity | Set-SCSMObject -Property Status -Value $Cancelled}}
If($SRequest.Status -eq $SRStatusCancelled){$SRequest |Set-SCSMObject -Property ImplementationResults -Value $SRImpStatusCancelled} else {$SRequest |Set-SCSMObject -Property ImplementationResults -Value $SRImpStatusFailed}

It works but it seems that when the SR contains multiple activities, the Completed Date is updated multiple times every minute or so and never stops till i restart the HealthService.

I tried moving the line 

$SRequest| Set-SCSMObject -Property CompletedDate -Value $Date

in different places in the script but its seems to be always the same result.

What is the problem?Also, is it normal that the time of the Completed date is 5h behind EST time?

Would it be possible to retrieve & set the SLA Cancel value to "yes" instead of having to update the Completed date? I know that this is set to Cancel = "yes" when your SLO is updated (ie change priority).


February 25th, 2015 1:11pm

You should add a check to see if the SR is already closed.  I looks as if the workflow is being triggered every minute.
Free Windows Admin Tool Kit Click here and download it now
February 25th, 2015 1:26pm

You should add a check to see if the SR is already closed.  I looks as if the workflow is being triggered every minute.

@Foothill1

Sorry im still learning powershell and SCSM. What would be the code to check? I thought my last "if" statement was a sort of check.

February 25th, 2015 1:44pm

I also tried adding this to the last line

If($SRequest.Status -eq $SRStatusCancelled -xor $SRequest.Status -eq $SRStatusFailed) {$SRequest| Set-SCSMObject -Property CompletedDate -Value $Date}



Free Windows Admin Tool Kit Click here and download it now
February 25th, 2015 4:55pm

I'm sorry, I can't be much help with the Powershell commands.  I offloaded my Activity status check to an Orchestrator run book that is executed whenever a ticket is closed.  With Orchestrator and the Service Manager SDK, I've haven't had much use for Powershell.

If need be, you could change the trigger from periodic to when an SR changes status from "not equal" "completed" to "equal" "completed."  This way it only triggers once.

February 25th, 2015 5:32pm

Hi

You need another filter on the query as you only want to set the completed date on SR's which do not have a completed date set. You will need to put an and statement in the powershell to include srequest.completeddate -eq NULL which should then only return the requests with no completed date.

The other option is to simply update the cancelled/failed requests to closed when setting the completed date

Free Windows Admin Tool Kit Click here and download it now
February 26th, 2015 4:53am

Hi

You need another filter on the query as you only want to set the completed date on SR's which do not have a completed date set. You will need to put an and statement in the powershell to include srequest.completeddate -eq NULL which should then only return the requests with no completed date.

The other option is to simply update the cancelled/failed requests to closed when setting the completed date

February 26th, 2015 9:50am

Hi

You need another filter on the query as you only want to set the completed date on SR's which do not have a completed date set. You will need to put an and statement in the powershell to include srequest.completeddate -eq NULL which should then only return the requests with no completed date.

The other option is to simply update the cancelled/failed requests to closed when setting the completed date

Free Windows Admin Tool Kit Click here and download it now
February 26th, 2015 9:50am

Thanks, that fixed :)
February 26th, 2015 12:49pm

Hi

You need another filter on the query as you only want to set the completed date on SR's which do not have a completed date set. You will need to put an and statement in the powershell to include srequest.completeddate -eq NULL which should then only return the requests with no completed date.

The other option is to simply update the cancelled/failed requests to closed when setting the completed date

Free Windows Admin Tool Kit Click here and download it now
February 26th, 2015 12:50pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics