Where-Object - 两个"相等"值

时间:2015-11-11 09:29:40

标签: powershell

如何将两个参数或值放入一个" -eq"在Where-Object?

我的代码如下:

$Drives | where {$_.Gruppe -eq $env:username} | foreach {
    $_.Gruppe = $UserOU
}

$Drives | where {$_.Gruppe -eq 'Alle'} | foreach {
    $_.Gruppe = $UserOU
}

但是,对我而言,我需要两个Scriptblocks并不合乎逻辑。我可以把它放在一行,像这样吗?

$Drives | where {$_.Gruppe -eq ($env:username -or 'Alle')} | foreach {
    $_.Gruppe = $UserOU
}

这会返回错误 - 但是这样的事情应该是可能的,对吗?

1 个答案:

答案 0 :(得分:1)

If you are using Powershell version 2 you should do it this way:

$Drives | where {$_.Gruppe -eq $env:username -or $_.Gruppe -eq 'Alle'} | foreach {
    $_.Gruppe = $UserOU
}

For Powershell version 3 or above you could use -in operator like this:

$Drives | where {$_.Gruppe -in $env:username,'Alle'} | foreach {
        $_.Gruppe = $UserOU
}