Windows Patches Installed On Date Incorrect Format on Windows Server 2012
If you run the following on Windows Server 2012
$patches = get-ciminstance Win32_QuickFixEngineering
$patches[0].InstalledOn
you get the date the patch was installed on in American date format MM/DD/YY. PowerShell then converts the DD part of the date into a month so the result is incorrect.
If you run the same query on Windows Server 2012 R2, the date is correct
If you dig into the code, you can see the issue, here it is in Windows Server 2012
And here it is in Windows Server 2012 R2
As you can see, the Windows Server 2012 R2 version has an additional parameter on the DateTime Parse method call which fixes the issue
[System.Globalization.DateTimeFormatInfo]::new()
$patches = get-ciminstance Win32_QuickFixEngineering
$patches[0].InstalledOn
you get the date the patch was installed on in American date format MM/DD/YY. PowerShell then converts the DD part of the date into a month so the result is incorrect.
If you run the same query on Windows Server 2012 R2, the date is correct
If you dig into the code, you can see the issue, here it is in Windows Server 2012
System.Object InstalledOn
{
get=if ([environment]::osversion.version.build -ge 7000)
{
# WMI
team fixed the formatting issue related to InstalledOn
#
property in Windows7 (to return string)..so returning the WMI's
# version
directly
[DateTime]::Parse($this.psBase.CimInstanceProperties["InstalledOn"].Value)
}
else
{
$orig
= $this.psBase.CimInstanceProperties["InstalledOn"].Value
$date
= [datetime]::FromFileTimeUTC($("0x" +
$orig))
if
($date -lt
"1/1/1980")
{
if
($orig -match
"([0-9]{4})([01][0-9])([012][0-9])")
{
new-object
datetime @([int]$matches[1], [int]$matches[2], [int]$matches[3])
}
}
else
{
$date
}
};
}
And here it is in Windows Server 2012 R2
System.Object InstalledOn
{
get=if ([environment]::osversion.version.build -ge 7000)
{
# WMI
team fixed the formatting issue related to InstalledOn
#
property in Windows7 (to return string)..so returning the WMI's
# version
directly
[DateTime]::Parse($this.psBase.CimInstanceProperties["InstalledOn"].Value, [System.Globalization.DateTimeFormatInfo]::new())
}
else
{
$orig
= $this.psBase.CimInstanceProperties["InstalledOn"].Value
$date
= [datetime]::FromFileTimeUTC($("0x" +
$orig))
if
($date -lt
"1/1/1980")
{
if
($orig -match
"([0-9]{4})([01][0-9])([012][0-9])")
{
new-object
datetime @([int]$matches[1], [int]$matches[2], [int]$matches[3])
}
}
else
{
$date
}
};
}
As you can see, the Windows Server 2012 R2 version has an additional parameter on the DateTime Parse method call which fixes the issue
[System.Globalization.DateTimeFormatInfo]::new()
Comments
Post a Comment