【问题标题】:Powershell: Find all computers in AD but exclude certain OU'sPowershell:查找 AD 中的所有计算机,但排除某些 OU
【发布时间】:2016-07-05 18:24:52
【问题描述】:

我正在尝试列出我域中的所有计算机,但存储在两个 OU 中的计算机除外。一旦我使用 -or 运算符添加第二个 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,*")}

【问题讨论】:

    标签: powershell active-directory ou


    【解决方案1】:

    你想要-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,*")}
    

    【讨论】:

    • 你能解释一下我为什么要使用 -and 吗?除非我在想这个错误,否则从逻辑上讲,如果 OU 名称不是“test”或“TIS”,则返回结果。似乎使用 -and 运算符不是我想要的(除非我想错了)
    • @tcox8 对于-and,它必须同时满足这两个条件才能成立。如果您使用-or,则一个条件为真满足操作员的要求,而另一个条件不需要测试。这是一个问题,因为计算机不能位于多个 OU 中。您要确保计算机不在两个 OU 中,因此必须分别评估两者。
    • @tcox8 您不是说“OU 名称不是 'test' 或 'TIS'。您是说“OU 名称不是 'test 并且 OU 名称不是 'TIS' "。看,你移动了"不"。如果你想使用-or,你必须说where { -not (($_.DistinguishedName -like "*OU=Test,*") -or ($_.DistinguishedName -like "*OU=TIS,*")) },这在逻辑上是相同的。见DeMorgan's Laws
    • 感谢您的澄清!
    猜你喜欢
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多