Why does 'continue' behave like 'break' in a Foreach-Object?

后端 未结 4 1329
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 04:25

If I do the following in a PowerShell script:

$range = 1..100
ForEach ($_ in $range) {
    if ($_ % 7 -ne 0 ) { continue; }
    Write-Host \"$($_) is a multi         


        
4条回答
  •  一生所求
    2020-11-27 04:59

    Another alternative is kind of a hack, but you can wrap your block in a loop that will execute once. That way, continue will have the desired effect:

    1..100 | ForEach-Object {
        for ($cont=$true; $cont; $cont=$false) {
            if ($_ % 7 -ne 0 ) { continue; }
            Write-Host "$($_) is a multiple of 7"
        }
    }
    

提交回复
热议问题