How to obtain DateClosed on IR

Apr 27, 2011 at 1:22 PM

I successfully used get-scsmincident to write a script that closes Resolved IR's that are 3 days inactive.

However, those IR's that achieve a status of Closed in this fashion do not show a ClosedDate in the DWDatamart.

Is there a way to use the current version of the smlets to perform the close workflow AND ensure a ClosedDate exists in the DWDatamart database?

Apr 27, 2011 at 5:06 PM

How are you closing the incident, i suspect that you're not setting the ClosedDate when you run your script. Be sure that you set the ClosedDate property on the incident and that should take care of it. As far as i know, this property isn't going to be automatically set

Apr 27, 2011 at 5:48 PM

I think all the examples of PowerShell scripts for this are not doing that.

$ResolvedIncidents |Set-SCSMIncident -Status Closed -Comment "Auto-closed after 5 days of inactivity"

$ResolvedIncidents |Set-SCSMObject -Property ClosedDate -Value Get-Date

Apr 27, 2011 at 5:58 PM

this certainly won't work as is, as it sets the ClosedDate property to the string "Get-Date", be sure to enclose get-date in parens

set-scsmobject-property ClosedDate -value (Get-Date)

Apr 27, 2011 at 5:59 PM

i'll look at updating set-scsmincident to also set the ClosedDate property if the incident is closed

Apr 27, 2011 at 6:25 PM

You're right about that, I'm actually using an internal function to get the GMT date and replaced the code here to not complicate things.

This is what I'm using, in case anybody is interested:


Import-Module -Name SMLets

function PrintMsg ($msg) {
    $LogDate = Get-Date -f "yyyy-MM-dd HH:mm:ss"
    Write-Host "$LogDate - $msg"

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

function Get-IncidentsResolved
    $ResolvedStatusId = (Get-SCSMEnumeration IncidentStatusEnum.Resolved$).id
    $IncidentClass = Get-SCSMClass System.WorkItem.Incident$
    $CriteriaType = "Microsoft.EnterpriseManagement.Common.EnterpriseManagementObjectCriteria"
    $NumDaysAgo = (Get-GMTDate).AddDays(-$CLOSEDAYS)
    $CritString = "Status = '$ResolvedStatusId' and ResolvedDate < '$NumDaysAgo' and ResolvedDate Is Not Null"
    $Criteria = New-Object $CriteriaType $CritString, $IncidentClass
    Get-SCSMObject -Criteria $Criteria

PrintMsg "Reviewing incidents to auto-close..."
$ResolvedIncidents = Get-IncidentsResolved

if ($ResolvedIncidents -ne $null) {
    $GMTDate = Get-GMTDate
    $ResolvedIncidents |Set-SCSMIncident -Status Closed -Comment "Auto-closed after $CLOSEDAYS days of inactivity"
    $ResolvedIncidents |Set-SCSMObject -Property ClosedDate -Value $GMTDate
    $NumInc = $ResolvedIncidents.Count
    PrintMsg "$NumInc incidents auto-closed after $CLOSEDAYS days of inactivity"
} else {
    PrintMsg "No incidents to auto-closed because of inactivity"

Apr 27, 2011 at 6:26 PM
jtruher wrote:

i'll look at updating set-scsmincident to also set the ClosedDate property if the incident is closed

That'd be great! :)

Apr 29, 2011 at 7:34 AM

i looked at this again, the latest version already sets the closed date, so I'm confused about what's going wrong in the datamart.
You can see that the ClosedDate property is set by inspecting the projection object ClosedDate property.

PS> $p = get-scsmincident -Title "Magna at aliquyam eum ea voluptua"
PS> $p | Set-SCSMIncident -status closed
PS> $p2 = get-scsmincident -Title "Magna at aliquyam eum ea voluptua"
PS> $p2.Object.ClosedDate
Friday, April 29, 2011 12:27:34 AM


Apr 29, 2011 at 1:00 PM

When I query the DWDataMart, I get NULL for the closedDate, for those IR's that were auto-closed. I get a non-NULL value for the ClosedDate for IR's that were not auto-closed.

use DWDataMart
, m.ClosedDate
from IncidentDimvw m
where m.Status = 'IncidentStatusEnum.Closed'