2016-07-05 104 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) –

相關問題