Search code examples
powershellpowershell-2.0

Exit loop from function


I want to break the following loop from a function that was called kind of like shown below.

Function test {
    $i++
    IF ($i -eq 10) {continue}
}

$Computers = "13","12","11","10","9","8","7","6","5","4","3","2","1"
ForEach ($Computer in $Computers) {
    Test
    write-host "Fail"
}

Solution

  • If I'm following you...

    Function test {
        $args[0] -eq 10
    }
    
    $Computers = "13","12","11","10","9","8","7","6","5","4","3","2","1"
    ForEach ($Computer in $Computers) {
        if(Test $Computer) {break} else {$Computer}
    }