I have a couple of Service Manager customers that want to automatically close incidents one day after they are resolved by a analyst. It will save them some manually work. In this post I will show you how to build a workflow that does this. The following example will close incidents that has been in resolved state for two hours. This workflow require smlets that you can download here.
- Start the Service Manager Authoring Tool.
- Create a new management pack, for example Contoso.AutoClose
- Create a new workflow
Name: ContosoAutoCloseWF
Run at a scheduled time or at scheduled intervals
Other interval, 2 hours - Drag and drop the “Windows PowerShell Script” activiti from the toolbox to your workflow
- In the Details pane, click “Script body”
- Input the following script in the script bodyGet-SCSMIncident -Status Resolved -InactiveFor 02:00:00 | Set-SCSMIncident -Status Closed -comment “Closed due to duration of resolved period”The 02:00:00 is a TimeSpan declaration where 2 is the number of hours by hh:mm:ss. Observe that this will use the “lastmodified” timestamp for comparison and not the “resolved date”.
- On the script properties tab, input smcomlets as windows powershell snap-in required by the script
- Copy the ContosoAutoCloseWF.dll to the C:\Program Files\Microsoft System Center\Service Manager 2010 directory on your Management Server
- Import the management pack into Service Manager
- After a couple of hours, navigate to Administration/Workflows/Status and verify that your workflow is working
- Done!
Your workflow will now automatically close all incidents that has been in resolved state for at least two hours, without any update. It will also add a comment to the action log saying “Closed due to duration of resolved period”. If you get any problems with your workflow the look at Travis post about troubleshooting workflows here.
You can download a example of this here AutoClose.
Update: Please note that SMCOMLETS is now named SMLETS. When you import this module or load it on the script properties page, make sure to use the correct name.
Update: Before start working with the cmdlets, please read this blogpost Properly Querying SCSM Using SMLets Get-SCSMObject cmdlet
Hi, thanks for reading my blog 🙂
If I was going to do that today, I would use SC Orchestrator, it is easier then the Authoring tool.
Not sure if this page is still active but I am trying to do this for SM 2016. In the authoring tool, I don’t see the properties tab in step 7 nor do I understand exactly what I would put there. I also don’t see the directly for teh dll file under the new 2016 schema. Can you help?
Hi all
I have another solution for this that also works perfect. See my blogpost here –> http://blog.scsmfaq.ch/2011/01/12/auto-close-resolved-incidents/
regards
Marcel
Hello
The script runs vell if execuded in Powershell
but if it runs with the workflow
i get this error looking in sql job log
02-12-2010 08:47:37
Set-SCSMIncident : Cannot validate argument on parameter ‘Impact’. The argument
“days” does not belong to the set “Low,Medium,High” specified by the ValidateS
et attribute. Supply an argument that is in the set and then try the command ag
ain.
At line:22 char:37
+ $ResolvedIncidents |Set-SCSMIncident <<<< -Status Closed -comment ?Auto-clos
ed after $CLOSEDAYS days of inactivity?
+ CategoryInfo : InvalidData: (:) [Set-SCSMIncident], ParameterBi
ndingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,SMLets.SCSMInci
dentSet
Note: i use the script in one of the comments
@TobHan: This code works for closing incidents after 5 days of resolved:
$CLOSEDAYS = 5
Function Get-GMTDate () {
$LocalZone = [System.TimeZoneInfo]::Local
$Hours = [system.Math]::Abs($LocalZone.BaseUtcOffset.Hours)
$Mins = [System.Math]::Abs($LocalZone.BaseUtcOffset.Minutes)
if ($LocalZone.IsdaylightSavingTime([system.DateTime]::Now)) { $Hours -= 1 }
$TimeDiff = New-Object TimeSpan 0,$Hours,$Mins,0,0
(Get-Date).Subtract($TimeDiff)
}
Import-Module -Name SMLets
$GMTDate = Get-GMTDate
$ResolvedStatus = Get-SCSMEnumeration IncidentStatusEnum.Resolved$
$ResolvedIncidents = Get-SCSMObject System.WorkItem.Incident$ |?{ $_.Status -eq $ResolvedStatus -and ($_.ResolvedDate).AddDays($CLOSEDAYS) -lt $GMTDate }
$ResolvedIncidents
if ($ResolvedIncidents -ne $null) {
$ResolvedIncidents |Set-SCSMIncident -Status Closed -comment “Auto-closed after $CLOSEDAYS days of inactivity”
$NumInc = $ResolvedIncidents.Count
Write-Host “$NumInc incidents auto-closed after $CLOSEDAYS days of inactivity”
} else {
Write-Host “No incidents to auto-closed because of inactivity”
}
Hi Anders,
I’m having the same issue as Alex…
From a Powershell Console the incidents are getting closed.
But when I include the same code into a workflow… it runs fine (workflow status is succeded with no error message) but the incidents remain the same.
I had the same issue with other code that I’ve used… it seems I’m going to have to run the scripts as a Windows Scheduled Task unless I find a solution.
Any ideas?
Thanks & Regards,
German.
Using this article as a basis, we created a brief video on how to create a management pack with PowerWF, that auto-closes and emails a report of the closed incidents.
http://www.youtube.com/watch?v=G-K28OBiRfI
Using this article and PowerWF this video demostrates creating a Management Pack with an email report of the auto-closed incidents
http://www.youtube.com/watch?v=G-K28OBiRfI
Hi, first, post it on the codeplex page as a request for the next version. You could build a custom workflow in the authoring tool for Service Manager, that first get-incidents and then look for older incidents. If you have Opalis you could soon use the Service Manager IP to do this. The IP will be released during Q4.
I want to auto close Incidents after 5 days. The -InactiveFor only allow up to 24 hours. Any ideas how to do that?
Tobias
[…] Auto Close Incidents (New) […]
Same issue, I can run in the PS console but can’t create a Management Pack and dll.
“invalid character in the given encoding. Line 2, position 141”
What are we missing?
Have you seen that the cmd is really doing something on your incidents?
Anders,
I’ve tried to re-create this step by step but it doesn’t work and I do not get an error message in the workflow.
When I start the Script Body manually (in PS Console) it works
“Get-SCSMIncident -Status Resolved -Inactive 00:30:00 | Set-SCSMIncident -Status Closed -Comment “Closed automatically”.
Included in a Management Pack: no chance. I do not get an error message. Under script properties I’ve tried ‘SCMmdletSnapin’ and also ‘smcomlets’ as mentioned in your tip here. Re-Importing the MP and trying it again does not affect the incidents to be changed (even there are such incidents). Would could be the problem?
Thank you very much for your help in advance.
Alex
[…] This post was mentioned on Twitter by FAQShop, FAQShop. FAQShop said: [http://www.contoso.se] Auto Close Incidents: I have a couple of Service Manager customers that want to automatically clo… http://bit.ly/ad8bGL […]