I need help with readin from an XML file

Welcome Forums General PowerShell Q&A I need help with readin from an XML file

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years ago.

  • Author
  • #5619

    Points: 0
    Rank: Member

    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) {

    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) {

    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) {


    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.


    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]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">


    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:

    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) {
    foreach ($ComputerNode3 in $xml.Report.list3.list3_ListControl_Collection.list3_ListControl) {

The topic ‘I need help with readin from an XML file’ is closed to new replies.