Reputation: 1
Thanks for taking a look at my question.
I am using powershell to try to parse through an xml file and get only the information I am interested in. Having looked around the net for some tutorials and examples I put together the command below, but it returns no result at all, probably because I am not targeting the information correctly. I can get information out of the file only in $xml.EnergyReport.Troubleshooter, but anything deeper than that like returns no results, like $xml.EnergyReport.Troubleshooter.AnalysisLog.LogEntry
This is my first time trying to parse an XML file. I am trying to automate getting the battery life information for a laptop battery to avoid having to scroll through a huge html/xml document.
The command I have cobbled together from the net is:
$Path = "c:\Users\Public\Desktop\powerreport.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
I am hoping the output will look something like this:
FYI the output is from the command
powercfg -energy -xml -output %public%\Desktop\powerreport.xml
I want to get the information from under the Troubleshooter element named Battery.
Below is an edited sample of the xml file from my local machine:
<?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>
Upvotes: 0
Views: 502
Reputation:
Try out the following script, which declares the XML in-line using a PowerShell "here string."
$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;
}
The output from this script looks like the following:
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
The important part of the script is where you're drilling down into the XML nodes. You kinda have to do it step-by-step, to make sure you don't miss something important.
$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;
}
Upvotes: 0