Windows 远程管理脚本返回 XML 而不是对象。 XML 不采用人工可读格式。 可以使用 MSXML API 和预安装的 XSL 文件的方法将数据转换为可读格式。
有关 WinRM XML 输出和原始和格式化 XML 的示例的详细信息,请参阅 Windows 远程管理中的脚本。
Winrm 命令行工具附带一个名为 WsmTxt.xsl 的转换文件,该文件以表格形式显示输出。 如果脚本将此文件提供给执行转换的 MSXML 方法,则输出与 Winrm 工具的输出相同。
设置原始 XML 输出的格式
创建 WSMan 对象并创建会话。
Set Wsman = CreateObject("Wsman.Automation") Set Session = Wsman.CreateSession创建表示 XML 响应输出和 XSL 转换的 MSXML 对象。
Set xmlFile = CreateObject( "MSXml.DOMDocument" ) Set xslFile = CreateObject( "MSXml.DOMDocument" )通过 Session 对象方法获取数据。
xmlResponse = Session.Get("http://schemas.microsoft.com/" & _ "wbem/wsman/1/wmi/root/cimv2/Win32_Service?Name=Spooler")提供 MSXML loadXML 方法的响应和用于存储转换文件的 加载 方法。
xmlFile.LoadXml(xmlResponse) xslFile.Load("WsmTxt.xsl")使用 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 远程管理 的