0

I need to get a remote host date & time including milliseconds using WMI.

The following isn't good enough:

Get-WmiObject Win32_UTCTime -ComputerName MY_REMOTE_HOST

As milliseconds are NOT implemented in the Win32_CurrentTime class which is derived by the Win32_UTCTime class:

Milliseconds

Data type: uint32

Access type: Read-only

Not implemented.

This property is inherited from Win32_CurrentTime.

(https://learn.microsoft.com/en-us/previous-versions/windows/desktop/wmitimepprov/win32-currenttime)

I'd appreciate any other suggestions to obtain this information using WMI from either Powershell or C#.

Community
  • 1
  • 1
ktopaz
  • 77
  • 8

3 Answers3

3

The WMI class win32_operatingsystem contains a property named LocalDateTime which also contains the milliseconds. You need to convert them though.

Simple sample :

$OS = Get-WmiObject win32_operatingSystem
$OS.ConvertToDateTime($OS.LocalDateTime)

The output is a DateTime object which also contains the milliseconds.

bluuf
  • 936
  • 1
  • 6
  • 14
  • This seems to work but I wonder - how is it possible that we extract time from an object that does not appear to have a time field inside? I just ran "Get-WmiObject Win32_operatingsystem -ComputerName MY_REMOTE_HOST" and the fields returned are:(SystemDirectory,Organization,BuildNumber,RegisteredUser,SerialNumber,Version) – ktopaz Dec 17 '18 at 15:28
  • By default a lot of the WMI objects you request in Powershell show only a subset of the properties (because Powershell has a lot of "templates" built in to filter out the data). By piping the command to fl * (fl is an alias for format-list) you can see them all. Using Get-Member you can also see the properties and datatypes (but you can't see the values). – bluuf Dec 17 '18 at 15:30
2

@bluuf's answer is the one you should use, but as a more complicated alternative, you can get the current date/time from the performance counter WMI classes like this:

$perfTime = Get-CimInstance -Query "Select * FROM Win32_PerfRawData_PerfOS_Processor WHERE Name = '0'"
(Get-Date "01/01/1601").AddTicks($perfTimeime.Timestamp_Sys100NS)
boxdog
  • 7,894
  • 2
  • 18
  • 27
1

Try PowerShell remoting:

$sess = New-PSSession -Credential (Get-Credential)
$dateTime = Invoke-Command -Session $sess -ScriptBlock { Get-Date -Format "o" }

To enable remoting see this link.

Hope that helps.

Moerwald
  • 10,448
  • 9
  • 43
  • 83