从函数退出循环

时间:2013-02-03 07:28:04

标签: powershell powershell-v2.0

我想从一个被称为如下所示类型的函数中打破以下循环。

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"
}

1 个答案:

答案 0 :(得分:3)

如果我跟着你...

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}
}