我有一个包含排除项的数组:
$hosts= @("*iphone*","*Samsung*")
我想从我的dhcp-lease中排除此主机:
Get-DhcpServerv4Lease -ComputerName $dhcp_server -ScopeId $scope.ScopeID |ForEach-Object {
if(($_) -and $_.Hostname -notlike $hostnames -and $_.ClientID -notlike $clientids -and $_.Description -notlike $descriptions) {
do something
当我在没有数组的情况下工作时:
$leases += Get-DhcpServerv4Lease -ComputerName $dhcp_server -ScopeId $scope.ScopeID |
where {$_.Hostname -notlike "*iphone*" -and $_.Hostname -notlike...
这已经可以了。
所以我不知道,当排除项位于数组而不是where子句中时,为什么这不起作用
答案 0 :(得分:0)
AFAIK,您不应使用-notlike
与数组进行比较。
您可以使用正则表达式运算符-notmatch
进行操作。为此,可以将数组中的选项与正则表达式 OR 字符|
组合在一起。
$hosts= ("iphone","Samsung" | ForEach-Object { [Regex]::Escape($_) }) -join '|'
得出iphone|Samsung
。
请注意使用[Regex]::Escape()
来确保正则表达式特殊字符转义,以便将它们视为文字。
那样,使用如下的Where-Object子句:
Where-Object {$_.Hostname -notmatch $hosts}
对$clientids
和$descriptions
做同样的事情