Почему ведет себя ли 'continue' как 'break' в Foreach-Object?

Если я сделаю следующее в сценарии PowerShell:

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

Я получаю ожидаемый результат:

7 is a multiple of 7
14 is a multiple of 7
21 is a multiple of 7
28 is a multiple of 7
35 is a multiple of 7
42 is a multiple of 7
49 is a multiple of 7
56 is a multiple of 7
63 is a multiple of 7
70 is a multiple of 7
77 is a multiple of 7
84 is a multiple of 7
91 is a multiple of 7
98 is a multiple of 7

Однако, если я использую конвейер и ForEach-Object , continue , похоже, вырывается из цикла конвейера.

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

Могу ли я получить поведение, подобное continue , при выполнении ForEach-Object, чтобы мне не пришлось разбивать конвейер?

112
задан Peter Mortensen 1 October 2019 в 16:21
поделиться