获取属于且不属于某个组的计算机

时间:2014-03-06 14:30:29

标签: powershell active-directory

我正在尝试使用下面的代码来获取不是InternetExplorer9组成员的计算机名列表。它运行没有错误,但它仍然给我计算机名称是InternetExplorer9的成员。知道我做错了什么吗?谢谢

Get-ADComputer -Property * -Filter * -Server DomainController -SearchBase "OU=City,OU=US,DC=Domain,DC=net"  | where {$_.memberof -notmatch 'InternetExplorer9'} | FT Name, memberof

1 个答案:

答案 0 :(得分:0)

试试这个:

Get-ADComputer -Property * -Filter * -Server DomainController -SearchBase "OU=City,OU=US,DC=Domain,DC=net"  |
 where {-not ($_.memberof -match 'InternetExplorer9')} | FT Name, memberof

-match和-notmatch,当用于数组时将返回满足测试的数组的所有元素,所以

$_.memberof -notmatch 'InternetExplorer9'

将返回与“InternetExplorer9”不匹配的所有组成员身份。当用作布尔测试时,如果存在任何不匹配的组,则表示它为真。

Where-Object将通过Where子句/ scriptblock运行当前管道对象,并将结果计算为Boolean(True / False)。假设您的MemeberOf集合中有3个组:

$groups = @('InternetExplorer9','SomeOtherGroup1','SomeOtherGroup2')
 $groups -notmatch 'InternetExplorer9'

SomeOtherGroup1
SomeOtherGroup2

-notmatch返回了与“InternetExplorer9”不匹配的所有群组成员资格。当它被评估为布尔值时,它将变为True,因为它找到了与“InternetExplorer9”不匹配的组,并且该对象将通过测试并继续沿着管道向下,即使它是'InternetExplorer9的成员'小组。它失败的唯一时间是它的所有组成员资格都匹配'InternetExplorer9',而-notmatch什么都不返回。

相关问题