I need help with readin from an XML file

by atkol29a at 2013-03-03 07:15:04

Hello Powershell community,

I have a monitoring system that I use for monitoring my client’s network. Once in a while my client asks for a report, and the report is good, but it has way too much information that the client needs. The report can be exported to different formats including XML, CSV, HTML, XLS, PDF, and DOC. Working with these formats and Powershell, I have found that it is easier for me to work with XML format. Here’s my problem:

Here’s an example Powershell code:
[code2=powershell][xml]$xml=Get-Content -Path "C:\Assessment\Hardware Inventory Summary.xml"

foreach ($compnode in $xml.Report.list1.list1_ListControl_Collection.list1_ListControl) {
$compname=($compnode.subreport1.Report.DeviceAssetTable.ComputerName)
$os=($compnode1.subreport1.Report.DeviceAssetTable.OperatingSystem)
}[/code2]

The code works great for what I need (in this example), but if you notice in the foreach line list1 and also subreport1. My problem is that the XML file also has other computers in list2, list3, and list4. How can I get all computers from every list?

Do I for example do something like:

[code2=powershell]foreach ($compnode1 in $xml.Report.list1.list1_ListControl_Collection.list1_ListControl) {
foreach ($compnode2 in $xml.Report.list2.list2_ListControl_Collection.list2_ListControl) {
foreach ($compnode3 in $xml.Report.list3.list3_ListControl_Collection.list3_ListControl) {
$compname1=($compnode1.subreport1.Report.DeviceAssetTable.ComputerName)
$compname2=($compnode2.subreport2.Report.DeviceAssetTable.ComputerName)
$compname3=($compnode3.subreport3.Report.DeviceAssetTable.ComputerName)
}
}
}[/code2]

Any help is greatly appreciated. Also, I use Powershell for AD administration so this is new to me. If you have a better way of doing this please feel free to point it out. Also, if the code formatting and/or method of getting what I need is not the best way, please let me know.

Best regards!
by DonJ at 2013-03-03 07:34:32
Yup. Multiple nested loops. You’re on the right track.
by atkol29a at 2013-03-03 09:56:04
for some reason the output is repeating.

[code2=powershell][xml]$xml=Get-Content -Path "c:\Assessment\Hardware Inventory Summary.xml"

foreach ($ComputerNode1 in $xml.Report.list1.list1_Details_Group_Collection.list1_Details_Group) {
foreach ($ComputerNode3 in $xml.Report.list3.list3_ListControl_Collection.list3_ListControl) {

$ComputerNode1.subreport1.Report.DeviceAssetTable.ComputerName
$ComputerNode1.subreport1.Report.DeviceAssetTable.OperatingSystem
$ComputerNode3.subreport3.Report.DeviceAssetTable.ComputerName
$ComputerNode3.subreport3.Report.DeviceAssetTable.OperatingSystem
}
}[/code2]

Here’s the output:

Computer001 (this is from list1)
Windows 7

Computer200 (this is from list3)
Windows XP

Computer001 (from list1)
Windows 7

Computer300 (from list3)
Server 2008

and so on…

Everything on list3 seems to be OK, but on list1 every computer repeats itself many times before going to second asset. Then the second asset does the same thing and repeats itself many times.

Thanks!
by DonJ at 2013-03-03 11:40:45
So, without seeing a small sample of the XML I can’t really help. Please don’t post the entire file, but if you can give me an idea of what it looks like?

The reason you’re getting repetition is because of the way you’ve nested the loops. But I can’t tell you what will work without seeing how the XML is structured.
by atkol29a at 2013-03-03 13:44:41
Hope this helps and thanks for taking the time to help me.

[code2=xml]<Report xsi:schemaLocation="Hardware_x0020_Inventory_x0020_Summary http://www.sitename.net" Name="Hardware Inventory Summary" textbox3="Hardware Inventory Summary" textbox4="2/1/2013 7:44:00 PM - 3/1/2013 7:44:46 PM" textbox12="Site Name:" Company="Microsoft" textbox9="Report Date:" textbox19="3/1/2013 6:47:21 PM">
<ReportLogoTable/>
<SiteInfoTable textbox83="Company" textbox84="Company Name" textbox86="Contact" textbox87="Contact Person"/>
<DeviceListTable textbox17="Total Devices In Site: 4"/>
<list1>
<list1_Details_Group_Collection>
<list1_Details_Group>
<subreport1>
<Report Name="Detailed Hardware Inventory Subreport">
<DeviceAssetTable textbox93="Computer001" textbox96="OS Type: Windows" textbox98="Configuration Information" textbox130="Site Name" textbox51="Discovery Date" textbox52="12/11/1969 4:45:53 PM" textbox53="IP Addresses" textbox54="127.0.0.1" textbox135="OS Description" textbox199="Microsoft Windows 7 Professional " textbox103="OS Version" textbox133="6.1.9999" textbox198="Role" textbox203="Member Workstation" textbox134="Service Pack" textbox184="0.0" textbox296="Vendor Warranty:" textbox192="4/28/2056" textbox313="Expires: 474566 days" textbox191="Custom Warranty:" textbox193="none" textbox303=""/>
<list1_Details_Group>
<subreport1>
<Report Name="Detailed Hardware Inventory Subreport">
<DeviceAssetTable textbox93="Computer002" textbox96="OS Type: Windows" textbox98="Configuration Information" textbox130="Site Name" textbox51="Discovery Date" textbox52="12/11/1969 4:45:53 PM" textbox53="IP Addresses" textbox54="127.0.0.1" textbox135="OS Description" textbox199="Microsoft Windows 7 Professional " textbox103="OS Version" textbox133="6.1.9999" textbox198="Role" textbox203="Member Workstation" textbox134="Service Pack" textbox184="0.0" textbox296="Vendor Warranty:" textbox192="4/28/2056" textbox313="Expires: 474566 days" textbox191="Custom Warranty:" textbox193="none" textbox303=""/>
</Report>

<list3>
<list3_ListControl_Collection>
<list3_ListControl>
<subreport3>
<Report Name="Detailed Hardware Inventory Subreport">
<DeviceAssetTable textbox93="Computer200" textbox96="OS Type: Other" textbox98="Configuration Information" textbox130="Site Name" textbox51="Discovery Date" textbox52="12/11/1969 4:45:53 PM" textbox53="IP Addresses" textbox54="8.8.8.8" textbox135="OS Description" textbox199="Windows XP" textbox103="OS Version" textbox198="Role" textbox203="Workstation" textbox134="Service Pack" textbox296="Vendor Warranty:" textbox192="none" textbox313="" textbox191="Custom Warranty:" textbox193="none" textbox303=""/>
<list3_ListControl>
<subreport3>
<Report Name="Detailed Hardware Inventory Subreport">
<DeviceAssetTable textbox93="Computer300" textbox96="OS Type: Other" textbox98="Configuration Information" textbox130="Site Name" textbox51="Discovery Date" textbox52="12/11/1969 4:45:53 PM" textbox53="IP Addresses" textbox54="8.8.8.8" textbox135="OS Description" textbox199="Windows XP" textbox103="OS Version" textbox198="Role" textbox203="Workstation" textbox134="Service Pack" textbox296="Vendor Warranty:" textbox192="none" textbox313="" textbox191="Custom Warranty:" textbox193="none" textbox303=""/>
</Report>[/code2]
by DonJ at 2013-03-04 04:01:09
Huh. Is the XML valid? List3 is embedded in List1? Making sure I’m reading it right. If it is, your loop logic should be basically right.
by atkol29a at 2013-03-04 07:20:14
Here’s what the XML structure looks like: [attachment=0]xml structure.PNG[/attachment]

I was able to get it to work.

I just put each list on their own foreach loop instead of nesting the foreach.

[code2=powershell]foreach ($ComputerNode1 in $xml.Report.list1.list1_Details_Group_Collection.list1_Details_Group) {
$ComputerNode1.subreport1.Report.DeviceAssetTable.ComputerName
$ComputerNode1.subreport1.Report.DeviceAssetTable.OperatingSystem
}
foreach ($ComputerNode3 in $xml.Report.list3.list3_ListControl_Collection.list3_ListControl) {
$ComputerNode3.subreport3.Report.DeviceAssetTable.ComputerName
$ComputerNode3.subreport3.Report.DeviceAssetTable.OperatingSystem
}[/code2]