通过 WMI 设置远程时间(调用 Win32_OperatingSystem.SetDateTime())

通过 WMI 设置远程时间(调用 Win32_OperatingSystem.SetDateTime())

目标是从 Python 和 Linux 发出 XML/SOAP/WSMan 请求并在远程 Windows 10 机器上设置时间。

我可以通过 CMD、PowerShell 或 Cygwin/SSH 执行此操作,但我需要快速的本地方式。我通过 WMI 执行了很多操作,它更简单、更快捷。

我尝试调用Win32_OperatingSystem.SetDateTime。SOAP
/WSMan 响应包含:The SOAP XML in the message does not match the corresponding XML schema definition. Change the XML and retry. (extended fault data: {u'fault_subcode': 'w:SchemaValidationError', u'fault_code': 's:Sender', u'wsmanfault_code': '2150858817', 'transport_message': u'Bad HTTP response returned from server. Code 500', 'http_status_code': 500})

我尝试在本地和远程机器上手动完成类似的任务。结果相同。

通过 WMIC:

C:\Users\Administrator>wmic /node:10.254.251.2 os call SetDateTime 20180517141043.945000+000
Executing (Win32_OperatingSystem)->SetDateTime()
ERROR:
Description = Invalid method Parameter(s)

通过 WinRM:

C:\Users\Administrator>winrm invoke SetDateTime wmicimv2/Win32_OperatingSystem @{LocalDateTime="20170505080808.123123+120"}
WSManFault
    Message
        ProviderFault
            WSManFault
                Message = The WS-Management service cannot process the request. The element for a datetime value must have exactly one child and no mixed content.

Error number:  -2144108479 0x80338041
The SOAP XML in the message does not match the corresponding XML schema definition. Change the XML and retry.

通过 PowerShell 的 WMI cmdlet:

PS C:\Users\Administrator> Invoke-WmiMethod -Class Win32_OperatingSystem -Name SetDateTime -ArgumentList '20180517133310.323710+000' -ComputerName 10.254.251.2
Invoke-WmiMethod : Invalid method Parameter(s) 
At line:1 char:1
+ Invoke-WmiMethod -Class Win32_OperatingSystem -Name SetDateTime -Argu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Invoke-WmiMethod], ManagementException
    + FullyQualifiedErrorId : InvokeWMIManagementException,Microsoft.PowerShell.Commands.InvokeWmiMethod

如果我更改字符串格式,我会得到Type Error。只有使用这种似乎正确的格式,我才会得到Invalid method Parameter(s)

Invalid method Parameter(s)当我从 执行时也得到WBEMTest

我探索了 的方法类型WBEMTest。参数类型是CIM_DATETIME。这有什么问题?如何更改远程机器上的时间?

如何通过 WMI 设置时间?

什么是“相应的模式定义”以及如何获取它?

答案1

通过 WMIC:我是 wmic 使用的新手,但我发现该命令具有如下过滤功能:

wmic os where(primary=1) call setdatetime 20070731144642.555555+480

我检查过了,确实有效。所以,我认为它应该与使用 进行远程调用相同/node。希望这对你有用。

代码来源如下:关联

答案2

我无法让它工作wmic,但这个 XML 对我有用:

<?xml version="1.0" ?>
<env:Envelope xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:b="http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd" xmlns:cfg="http://schemas.microsoft.com/wbem/wsman/1/config" xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:n="http://schemas.xmlsoap.org/ws/2004/09/enumeration" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:x="http://schemas.xmlsoap.org/ws/2004/09/transfer" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <env:Header>
    <w:OperationTimeout>PT00H02M00.000S</w:OperationTimeout>
    <a:To>http://windows-host:5985/wsman</a:To>
    <w:SelectorSet/>
    <w:MaxEnvelopeSize mustUnderstand="true">153600</w:MaxEnvelopeSize>
    <w:ResourceURI mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_OperatingSystem</w:ResourceURI>
    <a:Action mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_OperatingSystem/SetDateTime</a:Action>
    <p:DataLocale mustUnderstand="false" xml:lang="en-US"/>
    <a:ReplyTo>
      <a:Address mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
    </a:ReplyTo>
    <a:MessageID>uuid:52029873-fef3-4f08-ba32-57827df5fb2c</a:MessageID>
    <w:Locale mustUnderstand="false" xml:lang="en-US"/>
  </env:Header>
  <env:Body>
    <SetDateTime_INPUT xmlns:cim="http://schemas.dmtf.org/wbem/wscim/1/common" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_OperatingSystem" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <p:LocalDateTime>
        <cim:Datetime>2018-07-21T19:51:34.365502Z</cim:Datetime>
      </p:LocalDateTime>
    </SetDateTime_INPUT>
  </env:Body>
</env:Envelope>

Win32_OperatingSystem例如,这与 WMI 获取时响应的格式相同。

相关内容