Expanding Complex Nested Objects

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.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 (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.


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

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

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


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