在 Where 语句中使用数字范围

Using a Number Range in a Where Statement

我正在尝试在 Where 语句中使用数字范围,同时遍历 foreach 循环。 $i 变量用于根据循环经过的迭代次数来分隔循环的各个部分。

$a = 11..20
$i = 0
$PoolSW = ""
$PoolSW2 = ""
$PoolSW3 = ""

foreach ($Pool in $PoolTable) {
    $i++
    [Array]$PoolSW += "Statistic.Pool$($Pool.Name -replace "-","_"): $(Get-PoolHealth -BooleanState $Pool.Enabled)" | where {$i -le 10}
    [Array]$PoolSW2 += "Statistic.Pool$($Pool.Name -replace "-","_"): $(Get-PoolHealth -BooleanState $Pool.Enabled)" | where {$i -eq $a}
    [Array]$PoolSW3 += "Statistic.Pool$($Pool.Name -replace "-","_"): $(Get-PoolHealth -BooleanState $Pool.Enabled)" | where {$i -gt 20}
}

变量 $PoolSW$PoolSW3 工作正常,不幸的是我不知道如何让 $PoolSW2 正常工作。我试过 where {$i -eq 11..20} 和逗号分隔数字,我也试过 -contains

您可以使用这样的范围 -

... | where {$i -ge 11 -and $i -le 20}

使用-In运算符-

... | where {$i -in 11..20}