Bewerken

Delen via


Displaying XML Output from WinRM Scripts

Windows Remote Management scripts return XML rather than objects. The XML is not in a human-readable format. You can use the methods of the MSXML API and the preinstalled XSL file to transform the data into human-readable format.

For more information about WinRM XML output and examples of raw and formatted XML, see Scripting in Windows Remote Management.

The Winrm command-line tool comes with a transform file named WsmTxt.xsl that displays output in a tabular form. If your script supplies this file to the MSXML methods that perform tranforms, the output appears the same as the output from the Winrm tool.

To format raw XML output

  1. Create the WSMan object and create a session.

    Set Wsman = CreateObject("Wsman.Automation")
    Set Session = Wsman.CreateSession
    
  2. Create MSXML objects that represent the XML response output and the XSL transform.

    Set xmlFile = CreateObject( "MSXml.DOMDocument" )
    Set xslFile = CreateObject( "MSXml.DOMDocument" )
    
  3. Obtain data through Session object methods.

    xmlResponse = Session.Get("http://schemas.microsoft.com/" & _
        "wbem/wsman/1/wmi/root/cimv2/Win32_Service?Name=Spooler")
    
  4. Supply the response to the MSXML loadXML method and the load method to store the transform file.

    xmlFile.LoadXml(xmlResponse)
    xslFile.Load("WsmTxt.xsl")
    
    
  5. Use the MSXML transformNode method and display or save the output.

    Wscript.Echo xmlFile.TransformNode(xslFile)
    

The following VBScript code example shows the complete script.

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)

Adding a Portable Subroutine to Transform XML to Your Scripts

You can add a subroutine to your scripts that uses the preinstalled XSL file to convert the raw XML output from a WinRM script to a tabular form.

The following subroutine uses calls to the MSXML scripting methods to supply the output to 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

The following subroutine transforms each line of your data as shown in the following example.

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 

About Windows Remote Management

Using Windows Remote Management

Windows Remote Management Reference