4

Parsing remote machine local date and time with WMI

The other day I was asked to check the local date and time on some workstations in our network. The reason for that was time skew, and as I was told, some workstations’ clock was 1 hour behind. I launched PowerShell and wrote a quick script to get the raw WMI value of the local date and time on a list of remote machines and convert it to a readable DateTime object:

The result looked like:

I took a quick look at the last column and was able to determine that the time on the machines in question was correct. Right?

Wrong! Take a look at the result. Do you find anything unusual? Sharp-eyed among you probably noticed that there’s a one hour difference on pc1, between the hour in the LocalDateTime and the converted one (9 vs. 10). I was always under the impression that the DMTF conversion happens on the remote machine but apparently I was wrong.

The ConvertToDateTime method used in the calculated property is a script method added via ETS (Extended Type System) and under the cover it is using the [System.Management.ManagementDateTimeconverter]::ToDateTime method.

The description of the method in MSDN says:
Converts a given DMTF date time to DateTime. The returned DateTime will be in the current time zone of the system. 

Which raises the question: the current time zone of the remote machine or the time zone of the machine that executed the script? The ToDateTime method doesn’t support remote connections so conversion is done locally. This can, and does, introduce a “bug” or false results. No one really checks the value of LocalDateTime (guilty as charged); it’s too cryptic, and I have always counted on the result of the ConvertToDateTime method.

Now that I know that the conversion is made on the machine that executes the script I use one of the following to parse the LocalDateTime value (the Win32_LocalTime class is supported on XP/Windows Server 2003 and above):

Filed in: Articles, Did you Know? Tags: ,

4 Responses to "Parsing remote machine local date and time with WMI"

  1. Junz Tan says:

    OMG, this is what Im looking for, thank you so much. That is why I can’t get the exact time which using .ConvertToDateTime() or [Management.ManagementDateTimeConverter]

  2. Ben says:

    Funny, my very first attempt at writing a powershell script and I encounter this. Thanks!

  3. Denis St-Pierre says:

    Here is a modified version without the pipe-lining that prevent debugging.
    It uses the CurrentTimeZone property to correct AFTER the conversion.

    $computers = ‘pc1′,’pc2′,’pc3′
    ForEach ($computer in $computers) {
    $os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer

    $cdtvar=$os.ConvertToDateTime($os.LocalDateTime)
    $cdt2 = @{Name=’ConvertedDateTime2’;
    Expression={$cdtvar.AddMinutes($os.CurrentTimeZone) }}

    $os | Select-Object -Property CSName,LocalDateTime,$cdt2,Status
    }

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: