To automatically close incidents that have been resolved for a number of days is a pretty common request among customers who've been working with Service Manager in production for a while. Even though this can be handled manually, it's much more convenient to automate this process. The thought behind all of this is that when the Analysts change that status of the incident to resolved, the Affected User has a number of days to contact IT before the incident is closed. If the Affected User contacts IT before the incident is closed, the incident gets re-activated, otherwise a new incident has to be opened.
Make sure that SMLets and the Authoring Tool is installed and working properly and that you have read and understood these previous recipes in this chapter:
In this recipe, we will be using the SMLets to handle the actual automatic closure of resolved incidents. We will then put the PowerShell script in a custom workflow and schedule it to run once a day-just as we did in the previous recipe.
Import-Module SMLets $NumberOfDaysResolved = 7 $Now = (Get-Date).ToUniversalTime() $ResolvedDate = $Now.AddDays(-$NumberOfDaysResolved) $IncidentClass = Get-SCSMClass System.WorkItem.Incident$ $ResolvedStatusId = (Get-SCSMEnumeration IncidentStatusEnum.Resolved$).Id $CriteriaType = "Microsoft.EnterpriseManagement.Common. EnterpriseManagementObjectCriteria" $CriteriaString = "Status = '$ResolvedStatusId' and ResolvedDate <= '$ResolvedDate'" $Criteria = New-Object $CriteriaType $CriteriaString,$IncidentClass $ResolvedIncidents = Get-SCSMObject -Criteria $Criteria if ($ResolvedIncidents -ne $null) { $Propertyhash = @{"Status" = "Closed"; "ClosedDate" = $Now;} $ResolvedIncidents | Set-SCSMObject -PropertyHashtable $Propertyhash } Remove-Module SMLets
AutocloseResolvedIncidents.dll
to the installation folder of Service Manager on the management server. When that is done, import the management pack.The preceding steps are almost the exact same steps as in the previous recipe, what's interesting and new here is the actual script. So let's go through all the code:
Import-Module SMLets $NumberOfDaysResolved = 7 $Now = (Get-Date).ToUniversalTime() $ResolvedDate = $Now.AddDays(-$NumberOfDaysResolved)
As with most scripts, we start with importing the SMLets PowerShell module to be able to use all the commands. Then we set a variable to the number of days an incidents should have been resolved in order to be autoclosed. If you want the incidents to stay Resolved for another number of days, this is where you change that. Next we retrieve the current datetime in UTC format and then calculate the actual timestamp to use when looking for matching incidents to close.
$IncidentClass = Get-SCSMClass System.WorkItem.Incident$ $ResolvedStatusId = (Get-SCSMEnumeration IncidentStatusEnum.Resolved$).Id
Here we use the Get-SCSMClass
cmdlets to retrieve the incident class and the other line of code is to retrieve the GUID of the Incident resolved status enumeration. To get the actual enumeration object you can use the Get-SCSMEnumeration
cmdlets, but in this case we are only interested in the GUID and that's why we are just getting the ID.
$CriteriaType = "Microsoft.EnterpriseManagement.Common.EnterpriseManagementObjectCrit eria" $CriteriaString = "Status = '$ResolvedStatusId' and ResolvedDate <= '$ResolvedDate'" $Criteria = New-Object $CriteriaType $CriteriaString,$IncidentClass $ResolvedIncidents = Get-SCSMObject -Criteria $Criteria
This part of the code is pretty tricky. It is where we retrieve the Incidents with the Get- SCSMObject
cmdlet. But instead of using the -Filter
parameter, we are using another one called -Criteria
. The reason for this is because -Filter
can only handle one argument. So we could either retrieve the incidents with status Resolved or get the ones with a resolved date that is less than the one we calculated earlier. If you want to use several arguments when retrieving objects you will have to use the -Criteria
parameter. The criteria type should always be Microsoft.EnterpriseManagement.Common
. EnterpriseManagementObjectCriteria
, but you will have to change the criteria string to fit your needs. See a link to an official blogpost on where to read more about this under See also...
if ($ResolvedIncidents -ne $null) { $Propertyhash = @{"Status" = "Closed"; "ClosedDate" = $Now;} $ResolvedIncidents | Set-SCSMObject -PropertyHashtable $Propertyhash } Remove-Module SMLets
The final part of the script will check if the $ResolvedIncidents
variable contains any objects. If it does it will change the status of these to Closed and set the Closed Date. Finally, we unload the SMLets module from the PowerShell session.
Properly Querying SCSM Using SMLets Get-SCSMObject cmdlet
:https://blogs.technet.microsoft.com/servicemanager/2011/04/04/properly-querying-scsm-using-smlets-get-scsmobject-cmdlet/.