简体   繁体   中英

Using WMI from PowerShell or C# - how can I get remote host time, including milliseconds?

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://docs.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#.

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'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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM