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. XML 応答出力と XSL 変換を表す MSXML オブジェクトを作成します。

    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 ファイルを使用して、WinRM スクリプトからの生の XML 出力を表形式に変換するサブルーチンをスクリプトに追加できます。

次のサブルーチンでは、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 リモート管理リファレンス