I'm having some troubles with nested ForEach loops in Powershell. First, I need to iterate through list 1. For every object in list 1, I need to iterate through list 2. When I found the resembling object in list 2, I want to go to the next object in list 1.
I've tried break, i've tried continue, but it won't work for me.
Function checkLists() {
ForEach ($objectA in $listA) {
ForEach ($objectB in $listB) {
if ($objectA -eq $objectB) {
// Do something
// goto next object in list A and find the next equal object
}
}
}
}
a) What does a break/continue exactly do in PowerShell?
b) How exaclty should I conquer my 'problem'?
Here's an example using break/continue. Reverse the test in the inner loop, and use Continue to keep the loop going until the test fails. As soon as it gets a hit, it will break the inner loop, and go back to the next object in the outer loop.
I would use a Do..until loop - its intended use is exactly what you're describing.
Here's an easy example:
Use a label as described in
get-help about_break
:Like so,
This one took me awhile to finally get it correct. Hope the solution and the example help other folks.
In this app, I need to process each node and subnode as nested loops. The key seems to be getting the relative node names in the foreach loop.
Here's the code: