2016-07-05 100 views
0

我试图列出除我存储在两个OU中的任何计算机上的所有计算机。只要我使用-or操作符添加第二个OU,事情就会中断。Powershell:查找AD中的所有计算机,但排除某些OU的

使用这个我回所有计算机除了那些存储在我的第一不必要的OU:

[array]$Computers1 = get-adcomputer -filter '*' -SearchBase 'ou=WorkStations,dc=XXX,dc=XXX,dc=edu' | where { $_.DistinguishedName -notlike "*OU=Test,*" } 

添加-Or运营商造成不必要的计算机(那些存储在这两个OU的)被列入我的结果。

[array]$Computers1 = get-adcomputer -filter '*' -SearchBase 'ou=WorkStations,dc=XXX,dc=XXX,dc=edu' | where { ($_.DistinguishedName -notlike "*OU=Test,*") -or ($_.DistinguishedName -notlike "*OU=TIS,*")} 

回答

0

你想-and,不-or

[array]$Computers1 = get-adcomputer -filter '*' -SearchBase 'ou=WorkStations,dc=XXX,dc=XXX,dc=edu' | 
    where { ($_.DistinguishedName -notlike "*OU=Test,*") -and ($_.DistinguishedName -notlike "*OU=TIS,*")} 
+0

你能解释为什么我会用-and?除非我正在考虑这个错误,否则逻辑上我是说,如果OU名称不是“test”或“TIS”,则返回结果。似乎使用 - 和运算符不是我想要的(除非我想错) – tcox8

+0

@ tcox8使用'-and'必须满足两个条件才能成立。如果使用'-or',则一个条件成立满足操作员,另一个条件不需要测试。这是一个问题,因为计算机不能在一个以上的OU中。您要确保计算机不在OU中,因此都必须单独进行评估。 – Matt

+0

@ tcox8你不是说“OU的名称不是'test'或'TIS',你是说”OU名称不是'测试,OU名称不是'TIS'“。如果你想使用'-or',你必须说'where {-not(($ _。DistinguishedName -like“* OU = Test,*”) - 或者($ _。DistinguishedName - 就像“* OU = TIS,*”))}'这在逻辑上是一样的,参见[DeMorgan's Laws](https://en.wikipedia.org/wiki/De_Morgan%27s_laws) –

相关问题