I need help with readin from an XML file

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

  • Author
    Posts
  • #5619

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












    [/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]

You must be logged in to reply to this topic.