Forum Discussion
Issue with loop not completing as expected
Hi Mrworx91,
There's not enough information here for us to offer any real explanations. The loop on its own, devoid of any values doesn't help us understand what the issue might be.
Still, I can make some uneducated guesses - and a suggestion.
First, the suggestion: I'd put the exclamation point comparison ahead of the $FM comparison, since it's possible (if unlikely) that $FM could easily come after the exclamation point. If this were to happen, you'd be losing a count iteration to a comment when you perhaps are only looking to limit "real" configuration lines.
The leads to the next point - although I've having to make a lot of assumptions on this one: Remove the $counterinit-- from the exclamation block. Perhaps it's legitimate that you want this to impact $counterinit but it seems counterintuitive and is the most likely reason you're not seeing all ports configured, since comment lines (which begin with the exclamation point) are taking away your "quota" by decrementing $counterinit. For example, in a configuration where a lot of comments are front-loaded, you could in theory end up configuring zero ports because $counterinit has reached 0 before reaching any active configuration lines through having read nothing but comments.
Applying these guesses and suggestions would yield this similar code:
foreach ($port in $final) {
if ($counterinit -le 1) {
break
}
if ($port -match "!") {
$process.StandardInput.WriteLine("!")
$process.StandardInput.Flush()
} elseif ($port -match $FM) {
$process.StandardInput.WriteLine($port)
$process.StandardInput.WriteLine("device-tracking attach-policy ACCESS_IPDT")
$process.StandardInput.WriteLine("exit")
$process.StandardInput.Flush()
$counterinit--
}
}
The effect of this is that comments don't decrement your $counterinit value, meaning the initial value of 55 is fully allocated to active commands.
Cheers,
Lain