共用方式為


Windows 遠端管理中的腳本編寫

WinRM 中的 腳本 API 和隨附的適用於 C++ 的 COM API 是設計來密切反映 WS-Management 通訊協定的作業。

Windows 遠端管理中的 WinRM 指令碼 API 支援除了 WS-Management 之外的所有通訊協定作業。 它不允許訂閱事件。 若要訂閱 BMC 系統事件記錄檔中的事件,您必須使用 Wecutil 或 Wevtutil 命令行工具。 如需詳細資訊,請參閱 事件

WinRM 腳本 API 是由 Winrm.vbs 所呼叫,這是以 Visual Basic Scripting Edition (VBScript) 撰寫的命令行工具。 Winrm.vbs 提供如何使用 WinRM 腳本 API的範例。

使用 WSman 與 WMI 腳本的比較

WMI 透過 DCOM 連線到遠端電腦,需要配置如 連線到遠端電腦上的 WMI中描述的。 WinRM 不會使用 DCOM 連線到遠端電腦。 相反地,WS-Management 通訊協定會傳送SOAP訊息,而服務會使用單一埠來進行 HTTP 和 HTTPS 傳輸。

不同於 winrm 命令行工具,腳本必須提供傳遞至 WS-Management 通訊協定訊息所需的 XML。 它們也必須提供 URI。 如需更多資訊,請參閱 資源標識符Windows 遠端管理以及 WMI

WMI 文稿 API 適用於物件,例如代表電腦上資源的 Win32_LogicalDisk實例。 此 WMI 類別定義於 Managed 物件格式 (MOF) 檔案,這些檔案會以二進位格式儲存在 WMI 存放庫中。 在 WMI 中,單一資源的 Get 作業或多個實例的查詢會傳回 WMI 物件。

WinRM 腳本不會傳回物件,而是會傳回 XML 文字的數據流。 如需詳細資訊,請參閱 Windows 遠端管理和 WMI

顯示 WinRM 文稿的 XML 輸出

WinRM 腳稿 API 會取得並接收描述資源的 XML 字串。 產生的 XML 格式為文字數據流,而且需要以其他方式顯示 XML 轉換。

下列 WinRM 文本會產生原始 XML 輸出。

Set Wsman = CreateObject("Wsman.Automation")
Set xmlFile = CreateObject( "MSxml.DOMDocument")
Set Session = Wsman.CreateSession
Response = Session.Get("http://schemas.microsoft.com/wbem/wsman/" _
    & "1/wmi/root/cimv2/Win32_Service?Name=Spooler")
xmlFile.LoadXml(Response)
xmlFile.Save( "c:\RawOutput.xml")

下列文字區塊會顯示 WinRM 腳本的 XML 輸出。

<p:Win32_Service xmlns:xsi="https://www.w3.org/2001/XMLSchema-
instance" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1
/wmi/root/cimv2/Win32_Service" xmlns:cim="https://schemas.dmtf
.org/wbem/wsman/1/base" cim:Class="Win32_Service"><p:AcceptP
ause>false</p:AcceptPause><p:AcceptStop>true</p:AcceptStop>
<p:Caption>Print Spooler</p:Caption><p:CheckPoint>0</p:CheckP
oint><p:CreationClassName>Win32_Service</p:CreationClassName>
<p:Description>Loads files to memory for later printing</p:De
scription><p:DesktopInteract>true</p:DesktopInteract><p:Displ
ayName>Print Spooler</p:DisplayName><p:ErrorControl>Normal</p
:ErrorControl><p:ExitCode>0</p:ExitCode><p:InstallDate xsi:ni
l="true"/><p:Name>spooler</p:Name><p:PathName>C:\Windows\Syst
em32\spoolsv.exe</p:PathName><p:ProcessId>1720</p:ProcessId><
p:ServiceSpecificExitCode>0</p:ServiceSpecificExitCode><p:Ser
viceType>Own Process</p:ServiceType><p:Started>true</p:Starte
d><p:StartMode>Auto</p:StartMode><p:StartName>LocalSystem</p:
StartName><p:State>Running</p:State><p:Status>OK</p:Status><p
:SystemCreationClassName>Win32_ComputerSystem</p:SystemCreati
onClassName><p:SystemName>wsplab6-4</p:SystemName><p:TagId>0<
/p:TagId><p:WaitHint>0</p:WaitHint><cim:Location xmlns:cim="h
ttp://schemas.dmtf.org/wbem/wsman/1/base" xmlns:a="https://sc
hemas.xmlsoap.org/ws/2004/08/addressing" xmlns:w="https://sche
mas.dmtf.org/wbem/wsman/1/wsman"><a:Address>https://schemas.xm
lsoap.org/ws/2004/08/addressing/role/anonymous</a:Address><a:
ReferenceParameters><w:ResourceURI>https://schemas.microsoft.c
om/wbem/wsman/1/wmi/root/cimv2/Win32_Service</w:ResourceURI><
w:SelectorSet><w:Selector Name="Name">spooler</w:Selector></w
:SelectorSet></a:ReferenceParameters></cim:Location></p:Win32
_Service>

您的文稿可以使用 XML 轉換,讓此輸出更容易閱讀。 如需詳細資訊,請參閱 從 WinRM 腳本顯示 XML 輸出。

下列版本的腳本會將 XML 格式化為人類可讀取的輸出。

Set Wsman = CreateObject("Wsman.Automation")
Set xmlFile = CreateObject( "MSXml.DOMDocument" )
Set xslFile = CreateObject( "MSXml.DOMDocument" )

Set Session = Wsman.CreateSession
Response = Session.Get("http://schemas.microsoft.com/wbem/wsman/" _
    & "1/wmi/root/cimv2/Win32_Service?Name=Spooler")
xmlFile.LoadXml(Response)
xslFile.Load( "WsmTxt.xsl" )
Wscript.Echo xmlFile.TransformNode( xslFile )

XSL 轉換會建立下列輸出。

Win32_Service
    AcceptPause = false
    AcceptStop = true
    Caption = Print Spooler
    CheckPoint = 0
    CreationClassName = Win32_Service
    Description = Loads files to memory for later printing
    DesktopInteract = true
    DisplayName = Print Spooler
    ErrorControl = Normal
    ExitCode = 0
    InstallDate = null
    Name = Spooler
    PathName = C:\Windows\System32\spoolsv.exe
    ProcessId = 1720
    ServiceSpecificExitCode = 0
    ServiceType = Own Process
    Started = true
    StartMode = Auto
    StartName = LocalSystem
    State = Running
    Status = OK
    SystemCreationClassName = Win32_ComputerSystem
    SystemName = wsplab6-4
    TagId = 0
    WaitHint = 0

WinRM 腳本和 Winrm.cmd 輸出

WinRM 腳本的輸出會以 Unicode 編碼。 如果您建立 FileSystemObject 並從腳本寫入檔案,產生的檔案為 Unicode。 不過,如果您將輸出重新導向至檔案,編碼方式為 ANSI。 如果您將輸出重新導向至 XML 檔案,而且輸出中有 Unicode 字元,XML 將會無效。 請注意,Winrm 命令行工具會輸出 ANSI。

關於 Windows 遠端管理

使用 Windows 遠端管理

MSXSL

DOM 參考