简体   繁体   English

从PowerShell或C#使用WMI-如何获得远程主机时间(包括毫秒)?

[英]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. 我需要使用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: 由于在Win32_UTCTime类派生的Win32_CurrentTime类中未实现毫秒,因此:

Milliseconds 毫秒

Data type: uint32 数据类型:uint32

Access type: Read-only 访问类型:只读

Not implemented. 未实现。

This property is inherited from Win32_CurrentTime. 此属性是从Win32_CurrentTime继承的。

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

I'd appreciate any other suggestions to obtain this information using WMI from either Powershell or C#. 如果您有其他建议使用WMI从Powershell或C#获取此信息,我将不胜感激。

The WMI class win32_operatingsystem contains a property named LocalDateTime which also contains the milliseconds. WMI类win32_operatingsystem包含一个名为LocalDateTime的属性,该属性还包含毫秒。 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. 输出是一个DateTime对象,该对象还包含毫秒。

@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: @bluuf的答案是您应该使用的答案,但是作为更复杂的选择,您可以从性能计数器WMI类获取当前日期/时间,如下所示:

$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: 尝试PowerShell远程处理:

$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. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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