谢谢你看我的问题。
我正在使用powershell来尝试解析xml文件,并只获得我感兴趣的信息。在网上查找了一些教程和示例后,我将下面的命令放在一起,但它根本没有返回任何结果,可能是因为我没有正确地定位信息。我只能在$xml.EnergyReport中从文件中获取信息。但任何更深入的内容,如不返回结果,如$xml.EnergyReport.Troubleshooter.AnalysisLog。LogEntry
这是我第一次尝试解析XML文件。我正在尝试自动获取笔记本电脑电池的电池寿命信息,以避免在巨大的html/xml文档中滚动。
我从网上拼凑的命令是:
$Path = "c:UsersPublicDesktoppowerreport.xml"
$xml = New-Object -TypeName XML
$xml.Load($Path)
$item = Select-XML -Xml $xml -Xpath '//Troubleshooter[Name="Battery"]'
$item.AnalysisLog.LogEntry.Details.Detail | Select-Object -Property Name, Value
我希望输出看起来像这样:
- 电池ID 11111COMPANY111111
- 由COMPANY创建
- 序号11111
- 类型蝙蝠
- 电池设计容量62160
- 最后一次充满电36330
供参考,输出来自命令
powercfg -energy -xml -output %public%Desktoppowerreport.xml
我想从名为Battery的疑难解答元素下获取信息。
下面是我在本地机器上编辑的xml文件示例:
<?xml version="1.0" encoding="utf-8"?>
<EnergyReport xmlns="http://schemas.microsoft.com/energy/2007">
<ReportInformation>
<ReportGuid>dd3587c9-fe63-4e99-9695-2347b85477c0</ReportGuid>
<ReportVersion>1.0</ReportVersion>
<ScanTime>2014-03-14T06:42:19Z</ScanTime>
<ScanDuration>PT60S</ScanDuration>
</ReportInformation>
<Troubleshooter guid="5f159d5d-4dec-4caf-81e5-645d77e05c84">
<Name>Battery</Name>
<AnalysisLog>
<LogEntry guid="76e4b077-bb50-4000-9563-7f5aa0c9dc26">
<Name>Battery Information</Name>
<Severity>Informational</Severity>
<Description></Description>
<Details>
<Detail guid="118bf18a-13d4-4226-b207-f2ae1638de8b">
<Name>Battery ID</Name>
<Value>11111COMPANY111111</Value>
</Detail>
<Detail guid="85b01a9b-bb18-4f71-8d12-6f7dec4b3705">
<Name>Created by</Name>
<Value>COMPANY</Value>
</Detail>
<Detail guid="2229029f-aa9e-4591-989a-32223a114538">
<Name>Serial Number</Name>
<Value>11111</Value>
</Detail>
<Detail guid="24e6973f-f544-4a33-876d-359ebc56336e">
<Name>Type</Name>
<Value>BAT</Value>
</Detail>
<Detail guid="beb3f51a-9d89-42ad-81c4-5f9b7f682fa4">
<Name>Battery design capacity</Name>
<Value>62160</Value>
</Detail>
<Detail guid="b42aa79e-8ee8-44ae-8a11-5fe87cf2822b">
<Name>Last full charge</Name>
<Value>36330</Value>
</Detail>
</Details>
</LogEntry>
</AnalysisLog>
</Troubleshooter>
</EnergyReport>
尝试下面的脚本,该脚本使用PowerShell"here字符串"内联声明XML。
$Xml = @"
<?xml version="1.0" encoding="utf-8"?>
<EnergyReport xmlns="http://schemas.microsoft.com/energy/2007">
<ReportInformation>
<ReportGuid>dd3587c9-fe63-4e99-9695-2347b85477c0</ReportGuid>
<ReportVersion>1.0</ReportVersion>
<ScanTime>2014-03-14T06:42:19Z</ScanTime>
<ScanDuration>PT60S</ScanDuration>
</ReportInformation>
<Troubleshooter guid="5f159d5d-4dec-4caf-81e5-645d77e05c84">
<Name>Battery</Name>
<AnalysisLog>
<LogEntry guid="76e4b077-bb50-4000-9563-7f5aa0c9dc26">
<Name>Battery Information</Name>
<Severity>Informational</Severity>
<Description></Description>
<Details>
<Detail guid="118bf18a-13d4-4226-b207-f2ae1638de8b">
<Name>Battery ID</Name>
<Value>11111COMPANY111111</Value>
</Detail>
<Detail guid="85b01a9b-bb18-4f71-8d12-6f7dec4b3705">
<Name>Created by</Name>
<Value>COMPANY</Value>
</Detail>
<Detail guid="2229029f-aa9e-4591-989a-32223a114538">
<Name>Serial Number</Name>
<Value>11111</Value>
</Detail>
<Detail guid="24e6973f-f544-4a33-876d-359ebc56336e">
<Name>Type</Name>
<Value>BAT</Value>
</Detail>
<Detail guid="beb3f51a-9d89-42ad-81c4-5f9b7f682fa4">
<Name>Battery design capacity</Name>
<Value>62160</Value>
</Detail>
<Detail guid="b42aa79e-8ee8-44ae-8a11-5fe87cf2822b">
<Name>Last full charge</Name>
<Value>36330</Value>
</Detail>
</Details>
</LogEntry>
</AnalysisLog>
</Troubleshooter>
</EnergyReport>
"@
$Document = Select-Xml -Xml ([xml]$Xml) -XPath /;
$Details = $Document.Node.EnergyReport.Troubleshooter.AnalysisLog.LogEntry.Details.Detail;
foreach ($Detail in $Details) {
'Name: {0} Value: {1}' -f $Detail.Name, $Detail.Value;
}
这个脚本的输出如下所示:
Name: Battery ID Value: 11111COMPANY111111
Name: Created by Value: COMPANY
Name: Serial Number Value: 11111
Name: Type Value: BAT
Name: Battery design capacity Value: 62160
Name: Last full charge Value: 36330
脚本的重要部分是向下钻取XML节点的地方。你必须一步一步地做,以确保你没有错过重要的东西。
$Document = Select-Xml -Xml ([xml]$Xml) -XPath /;
$Details = $Document.Node.EnergyReport.Troubleshooter.AnalysisLog.LogEntry.Details.Detail;
foreach ($Detail in $Details) {
'Name: {0} Value: {1}' -f $Detail.Name, $Detail.Value;
}