Powershell 从函数退出循环

Powershell 从函数退出循环,powershell,powershell-2.0,Powershell,Powershell 2.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" } 如果我跟着你 Function test {

我想从一个函数中中断以下循环,该函数的调用类似于下面所示

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

如果我跟着你

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

如果我跟着你

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

你想干什么?如果要中断,请使用
break
关键字,而不是
continue
:)您想做什么?如果要中断,请使用
break
关键字,not
continue
:)是的,这就是我想要的,我只是想知道我是否可以从函数内部调用break,以便尽可能保持脚本的主体干净。是的,这就是我想要的,我只是想知道我是否可以从函数内部调用break,以便尽可能保持脚本的主体干净。