以下是我的代码片段(基于this previous question):
$rgdNames = (Get-AzureRmResourceGroupDeployment -ResourceGroupName "$azureResGrpName").DeploymentName
$siblings = $rgdNames | Where-Object{$_ -match "^($hostname)(\d+)$" }
if ($siblings) {
# Make a list of all numbers in the list of hostnames
$serials = @()
foreach ($sibling in $siblings) {
# $sibling -split ($sibling -split '\d+$') < split all digits from end, then strip off everything at the front
# Then convert it to a number and add that to $serials
$hostnumber = [convert]::ToInt32([string]$($sibling -split ($sibling -split '\d+$'))[1], 10)
$serials += $hostnumber
}
(1..$siblingsMax) | foreach { # Iterate over all valid serial numbers
if (!$serials.Contains($_)) { # Stop when we find a serial number that isn't in the list of existing hosts
$serial = $_
# break # FIXME: For some reason, this break statement breaks "too far"
}
}
} else {
$serial = 1
}
write-output("serial = $serial") # does not print
# ...more statements here, but they're never called :(
我已经看了一段时间,但无法弄清楚为什么break
语句(如果未注释)会停止我的程序而不是仅仅突破其foreach
循环。是否有一些我不知道的foreach
,或者break
的工作方式与Java中的工作方式不同?
目前,我正在使用额外的if
测试来解决这个问题,并不意味着(太多)循环运行它的整个长度。但它太丑了!
答案 0 :(得分:1)
这个结构:
(1..$siblingsMax) | foreach { # Iterate over all valid serial numbers
# do stuff
}
是 NOT 一个foreach
循环 - 它是一个调用ForEach-Object
cmdlet的管道(别名为{{1设计用于中断循环控制流的关键字(如foreach
或break
)在这两种不同的情况下不会以相同的方式起作用。
使用正确的continue
循环,break将按预期运行:
foreach
或者,您可以滥用continue
behaves like you would expect break
in a ForEach-Object
进程阻止:
foreach($i in 1..$siblingsMax){
# do stuff
if($shouldBreak)
{
break
}
}
虽然我会强烈阻止这种做法(它只会导致更多的混淆)