顯示 WinRM 文稿的 XML 輸出
Windows 遠端管理文本會傳回 XML,而不是 物件。 XML 不是人類可讀取的格式。 您可以使用 MSXML API 和預安裝 XSL 檔案的方法,將資料轉換成人類可讀取的格式。
如需 WinRM XML 輸出的詳細資訊,以及原始和格式化 XML 的範例,請參閱 windows 遠端管理中的 腳稿。
Winrm 命令行工具隨附名為 WsmTxt.xsl 的轉換檔案,以表格式格式顯示輸出。 如果您的腳本將此檔案提供給執行 tranforms 的 MSXML 方法,輸出會顯示與 Winrm 工具的輸出相同。
格式化原始 XML 輸出
建立 WSMan 物件並建立會話。
Set Wsman = CreateObject("Wsman.Automation") Set Session = Wsman.CreateSession
建立 MSXML 物件,代表 XML 回應輸出和 XSL 轉換。
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 方法和 load 方法的回應,以儲存轉換檔案。
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 檔案,將 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
相關主題