Auto Close Incidents

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.

  1. Start the Service Manager Authoring Tool.
  2. Create a new management pack, for example Contoso.AutoClose
  3. Create a new workflow
    Name: ContosoAutoCloseWF
    Run at a scheduled time or at scheduled intervals
    Other interval, 2 hours
  4. Drag and drop the “Windows PowerShell Script” activiti from the toolbox to your workflow
  5. In the Details pane, click “Script body”
  6. 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”.
  7. On the script properties tab, input smcomlets as windows powershell snap-in required by the script
  8. Copy the ContosoAutoCloseWF.dll to the C:\Program Files\Microsoft System Center\Service Manager 2010 directory on your Management Server
  9. Import the management pack into Service Manager
  10. After a couple of hours, navigate to Administration/Workflows/Status and verify that your workflow is working
  11. 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

13 thoughts on “Auto Close Incidents

  1. 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

  2. @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”
    }

  3. 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.

  4. 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.

  5. I want to auto close Incidents after 5 days. The -InactiveFor only allow up to 24 hours. Any ideas how to do that?

    Tobias

  6. 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?

  7. 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

Leave a Reply

Your email address will not be published. Required fields are marked *