在Where语句中使用数字范围

时间:2019-03-13 10:58:59

标签: powershell loops foreach numbers where

我尝试在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

1 个答案:

答案 0 :(得分:2)

您可以使用-

之类的范围
... | where {$i -ge 11 -and $i -le 20}

OR

使用-In运算符-

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