Expanding Complex Nested Objects

Tagged: 

This topic contains 3 replies, has 4 voices, and was last updated by  Curtis Smith 2 weeks, 2 days ago.

  • Author
    Posts
  • #79027

    Xeroize
    Participant

    I am working on a script that will take laboratory testing results in the form of XML and fire off specific scripts on remote machines based upon testing results. The issue I am running into is that when I convert the XML to a PSObject the test results are being imported as a nested object and I can't seem to get this to expand in an ideal way without losing the testing machine's name in the process.

    I have been searching for a solution for this for a couple of days now.

    Example Data Properties Being imported from XML (Changed for Obscurity):
    $_.devices.device.Name: Laboratory1
    $_.devices.device.IPAddress: 192.168.0.101
    $_.devices.device.MACAddress: (MAC Address)
    $_.devices.device.LabTestResults: This is the nested object, containing around 1000 items all named "LabTestResults", at the next level down, each "LabTestResults" has a unique ID, description, the test results (pass/fail/testing process failed), and the time the test was ran.

    I'm importing the XML in this fashion:

    [XML]$ReportListing = Get-Content 'C:\Example\Example.xml'
    

    The issue is that I need to be able to iterate through the test results across multiple machines, find cases where "testing process failed", and remotely kick off scripts that will fix known issues and restart the tests that failed (these scripts already exist), so I need to be able to return the name of the machine, and the IDs of tests that failed to provide as parameter arguments into the remediation scripts.

    The idea output would be something like:
    Name IPAddress MACAddress TestResult(ID) TestResult(OtherID) TestResult(3rdID)
    Labratory1 192.168.0.101 (MAC Address) Pass Fail Testing Process Failed

    so then I could theoretically do a "where-object" sort for finding cases that meet the criteria and grab the ID as well as the name.

    Any help would be greatly appreciated.

    Thanks,
    Xeroize

  • #79057

    Olaf Soyk
    Participant

    Do you have some more code to show? Where do you stuck?

  • #79063

    Kevyn
    Participant

    Would it be possible, also, for you to give us an outline of the XML data structure with a few example test results?

  • #79189

    Curtis Smith
    Participant

    I think I understand the challenge, but as previously requested, if you can provide some sample of what you are working with, it would help a lot.

    From my understanding, you have something similar to this:

    If so, you could do something like this:

    [xml]$xml = Get-Content "D:\input.xml"
    
    $xml.devices.device | Select-Object DeviceName, IPAddress, MACAddress -ExpandProperty LabTestResults | Where-Object {$_.'#text' -eq 'Fail'} | Sort-Object LabName | FT
    

    Results:

    DeviceName  IPAddress     MACAddress        LabName #text
    ----------  ---------     ----------        ------- -----
    Laboratory2 192.168.0.102 55:55:55:55:55:56 Test1   Fail 
    Laboratory3 192.168.0.103 55:55:55:55:55:57 Test1   Fail 
    Laboratory1 192.168.0.101 55:55:55:55:55:55 Test2   Fail 
    Laboratory3 192.168.0.103 55:55:55:55:55:57 Test2   Fail 
    Laboratory2 192.168.0.102 55:55:55:55:55:56 Test3   Fail 
    Laboratory1 192.168.0.101 55:55:55:55:55:55 Test4   Fail 
    Laboratory2 192.168.0.102 55:55:55:55:55:56 Test5   Fail 
    Laboratory3 192.168.0.103 55:55:55:55:55:57 Test5   Fail 

You must be logged in to reply to this topic.