2013-02-03 47 views
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" 
} 
+0

什么是你想怎么办?如果你想中断,请使用'break'关键字,而不是'continue' :) –

回答

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

是的,这就是我想要的只是想知道如果我可以从函数内部调用break,所以我可以保持脚本的主体清洁尽可能。 – user1451070