检测计算机是否不属于至少一个组
detect if computer is not member of at least one group
我需要检测不属于至少一个组的计算机对象。我想出了这个脚本,但它不是只列出不属于至少一个组的机器,而是 returns 所有工作站。我做错了什么?
Get-ADComputer -Filter * -Property * | where {
$_.memberof -notmatch 'Group1' -and
$_.memberof -notmatch 'Group2' -and
$_.memberof -notmatch 'Group3'
} | Format-Table Name
您正在使用 -NotMatch
运算符,如果条目不完全匹配,该运算符的计算结果为真。你最好使用 -NotContain
,像这样
Get-ADComputer -Filter * -Property * | where {$.memberof -notContains 'Group1' -and $.memberof -notContains 'Group2' -and $_.memberof -notContains 'Group3'} | Format-Table Name
MemberOf
属性 包含专有名称列表。您无法使用 -notmatch
运算符检查它是否不包含某些内容。取而代之的是获取您的组的专有名称列表:
$groups = 'Group1', 'Group2', 'Group3' |
ForEach-Object { Get-ADGroup -Filter "Name -eq '$_'" } |
Select-Object -Expand DistinguishedName
并检查 MemberOf
属性 是否不包含以下任何内容:
Get-ADComputer -Filter * -Property * | Where-Object {
-not (Compare-Object $groups $_.MemberOf -IncludeEqual -ExcludeDifferent)
} | Format-Table Name
Compare-Object
是必需的,因为您需要检查一个数组是否包含另一个数组的任何元素。 $_.MemberOf | Where-Object {$groups -contains $_}
之类的东西也可以。
请注意 MemberOf
属性 不 包括计算机的主要组。如果主要组也不能是您列表中的组之一,您需要在 Where-Object
过滤器中进行额外检查:
Get-ADComputer -Filter * -Property * | Where-Object {
-not (Compare-Object $groups $_.MemberOf -IncludeEqual -ExcludeDifferent) -and
$groups -notcontains $_.PrimaryGroup
} | Format-Table Name
我需要检测不属于至少一个组的计算机对象。我想出了这个脚本,但它不是只列出不属于至少一个组的机器,而是 returns 所有工作站。我做错了什么?
Get-ADComputer -Filter * -Property * | where {
$_.memberof -notmatch 'Group1' -and
$_.memberof -notmatch 'Group2' -and
$_.memberof -notmatch 'Group3'
} | Format-Table Name
您正在使用 -NotMatch
运算符,如果条目不完全匹配,该运算符的计算结果为真。你最好使用 -NotContain
,像这样
Get-ADComputer -Filter * -Property * | where {$.memberof -notContains 'Group1' -and $.memberof -notContains 'Group2' -and $_.memberof -notContains 'Group3'} | Format-Table Name
MemberOf
属性 包含专有名称列表。您无法使用 -notmatch
运算符检查它是否不包含某些内容。取而代之的是获取您的组的专有名称列表:
$groups = 'Group1', 'Group2', 'Group3' |
ForEach-Object { Get-ADGroup -Filter "Name -eq '$_'" } |
Select-Object -Expand DistinguishedName
并检查 MemberOf
属性 是否不包含以下任何内容:
Get-ADComputer -Filter * -Property * | Where-Object {
-not (Compare-Object $groups $_.MemberOf -IncludeEqual -ExcludeDifferent)
} | Format-Table Name
Compare-Object
是必需的,因为您需要检查一个数组是否包含另一个数组的任何元素。 $_.MemberOf | Where-Object {$groups -contains $_}
之类的东西也可以。
请注意 MemberOf
属性 不 包括计算机的主要组。如果主要组也不能是您列表中的组之一,您需要在 Where-Object
过滤器中进行额外检查:
Get-ADComputer -Filter * -Property * | Where-Object {
-not (Compare-Object $groups $_.MemberOf -IncludeEqual -ExcludeDifferent) -and
$groups -notcontains $_.PrimaryGroup
} | Format-Table Name