当我使用 URL 连接到我的 HP ILO 时,http://ilo-ip/xmldata?item=All 返回以下格式的 XML 文件。
<?xml version="1.0" ?>
- <RIMP>
- <HSI>
<SPN>ProLiant DL385 G1</SPN>
<SBSN>ABCDEFG</SBSN>
<UUID>123456789</UUID>
</HSI>
- <MP>
<ST>1</ST>
<PN>Integrated Lights-Out (iLO)</PN>
<FWRI>1.82</FWRI>
<HWRI>ASIC: 2</HWRI>
<SN>ILOABCDEFG</SN>
<UUID>ILO1234545</UUID>
</MP>
</RIMP>
是否有任何 powershell 命令/脚本可以从 web URL 读取 XML 数据?
最佳答案
PowerShell 使用 .Net 框架 XmlDocument,因此您可以像这样调用加载方法将 xml 加载到 XmlDocument 对象中:
#Option 1
$doc = New-Object System.Xml.XmlDocument
$doc.Load("http://ilo-ip/xmldata?item=All")
#Option 2
[xml]$doc = (New-Object System.Net.WebClient).DownloadString("http://ilo-ip/xmldata?item=All")
# Your XML will then be in the $doc and the names of the XML nodes can be used as
# properties to access the values they hold. This short example shows how you would
# access the value held in the SPN nodes from your sample XML
Write-Host $doc.RIMP.HSI.SPN
如果您正在寻找有关在加载 XML 后使用它的更多信息,请查看 Dr. Tobias Weltner's eBook在 PowerShell.com,chapter 14涵盖 XML。
关于xml - 如何使用 powershell 从 web 读取 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1819341/