powershell combine where and or



我试图构建一个脚本,根据用户的主要名称来排除用户。我已经开始使用以下脚本

Get-MsolUser -all | ?{($_.userprincipalname -notlike "*@domain1.com" -or $_.userprincipalname -notlike "*@domain2.com" -or $_.userprincipalname -notlike "*domain3.com")}

上面的代码没有排除任何用户,只是忽略了where语句

BRCarlos

例如,如果后缀是@domain2.com,则其他两个子句仍然是true(因为@domain2.com仍然是而不是@domain1.com@domain3.com(,因此您将希望使用-and而不是-or:

Get-MsolUser -all | ?{($_.userprincipalname -notlike "*@domain1.com" -and $_.userprincipalname -notlike "*@domain2.com" -and $_.userprincipalname -notlike "*domain3.com")}

其他解决方案,带有域列表:

$ExcludeDomain=@('domain1.com', 'domain2.com', 'domain3.com')
Get-MsolUser -all | where {($_.userprincipalname -split '@')[1] -notin $ExcludeDomain}

最新更新