如果我在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的同时还能获得类似的行为,所以我不必中断管道?
找到一份像样的解释和样品这里... powershell.com/cs/blogs/tips/archive/2015/04/27/...
—
弥敦道哈特利
foreach
:techotopia.com/index.php/…–