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。 如需詳細資訊,請參閱 資源 URIWindows 遠端系統管理和 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 參考