Share via


顯示 WinRM 腳本的 XML 輸出

Windows 遠端系統管理腳本會傳回 XML,而不是 物件。 XML 不是人類可讀取的格式。 您可以使用 MSXML API 和預先安裝的 XSL 檔案方法,將資料轉換成人類可讀取的格式。

如需 WinRM XML 輸出和原始和格式化 XML 範例的詳細資訊,請參閱 Windows 遠端系統管理中的腳本

Winrm命令列工具隨附名為 WsmTxt.xsl 的轉換檔案,以表格式形式顯示輸出。 如果您的腳本將此檔案提供給執行轉換的 MSXML 方法,則輸出會與 Winrm 工具的輸出相同。

格式化原始 XML 輸出

  1. 建立 WSMan 物件並建立會話。

    Set Wsman = CreateObject("Wsman.Automation")
    Set Session = Wsman.CreateSession
    
  2. 建立 MSXML 物件,代表 XML 回應輸出和 XSL 轉換。

    Set xmlFile = CreateObject( "MSXml.DOMDocument" )
    Set xslFile = CreateObject( "MSXml.DOMDocument" )
    
  3. 透過 Session 物件方法取得資料。

    xmlResponse = Session.Get("http://schemas.microsoft.com/" & _
        "wbem/wsman/1/wmi/root/cimv2/Win32_Service?Name=Spooler")
    
  4. 提供 MSXML loadXML 方法的回應,以及用來儲存轉換檔案的 load 方法。

    xmlFile.LoadXml(xmlResponse)
    xslFile.Load("WsmTxt.xsl")
    
    
  5. 使用 MSXML transformNode 方法,並顯示或儲存輸出。

    Wscript.Echo xmlFile.TransformNode(xslFile)
    

下列 VBScript 程式碼範例顯示完整的腳本。

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

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

將可攜式副程式新增至腳本以轉換 XML

您可以將副程式新增至腳本,以使用預先安裝的 XSL 檔案,將原始 XML 輸出從 WinRM 腳本轉換成表格式表單。

下列副程式會使用 MSXML 腳本方法的呼叫,將輸出提供給 WsmTxt.xsl。

'****************************************************
' Displays WinRM XML message using built-in XSL
'****************************************************
Sub DisplayOutput(strWinRMXml)
    Set xmlFile = CreateObject("MSXml.DOMDocument") 
    Set xslFile = CreateObject("MSXml.DOMDocument")
    xmlFile.LoadXml(strWinRMXml)
    xslFile.Load("WsmTxt.xsl")
    Wscript.Echo xmlFile.TransformNode(xslFile)
End Sub

下列副程式會轉換資料的每一行,如下列範例所示。

Const RemoteComputer = "servername.domain.com"
Set objWsman = CreateObject("WSMan.Automation")
Set objSession = objWsman.CreateSession("https://" & RemoteComputer)
strResource = "http://schemas.microsoft.com/" & _
    "wbem/wsman/1/wmi/root/cimv2/Win32_LogicalDisk"
Set objResultSet = objSession.Enumerate(strResource)
While Not objResultSet.AtEndOfStream
    DisplayOutput(objResultSet.ReadItem)
Wend
Sub DisplayOutput(strWinRMXml)
    Set xmlFile = CreateObject("MSXml.DOMDocument") 
    Set xslFile = CreateObject("MSXml.DOMDocument")
    xmlFile.LoadXml(strWinRMXml)
    xslFile.Load("WsmTxt.xsl")
    Wscript.Echo xmlFile.TransformNode(xslFile) 
End Sub 

關於 Windows 遠端系統管理

使用 Windows 遠端系統管理

Windows 遠端系統管理參考