【问题标题】:Count AD group members with certain properties计算具有某些属性的 AD 组成员
【发布时间】:2021-09-13 04:24:26
【问题描述】:

我需要一个 PowerShell 脚本来计算扩展属性 4 属性等于 o365_facstaff 的 AD 组的成员。

我一直在使用以下脚本来计算组的所有成员,但我特别需要具有属性的那些:

$ADInfo = Get-AdGroup -Identity ‘<group name>’ -Properties Members 
$ADInfo.Members.Count

【问题讨论】:

  • $ADInfo.Members |Where-Object {(Get-ADUser $_ -Properties extensionAttribute4).extensionAttribute4 -eq 'o365_facstaff'} |Measure-Object

标签: powershell directory office365


【解决方案1】:

我们可以通过对 Get-ADUser 的单个 -Filter 查询从 AD 获取此信息:

# We'll need the group DN instead of the group name
# Here's an example
$groupDn = 'CN=test_group,CN=Users,DC=bender,DC=net'

# Get all ADUsers member of the target group with the specific 
$groupMembers = Get-ADUser -Filter "(memberOf -RecursiveMatch '$groupDn') -and (extensionAttribute4 -eq 'o365_facstaff')"

# Check the Count property like you would with any array
$groupMembers.Count

或者,如 cmets 中所述,您可以将组成员从 ADGroup 中移除并进一步过滤,但这会导致额外的不必要的本地处理。对于非常大的组,这可能会成为问题,尤其是当您的 ADDS 基础架构运行接近最低系统要求时:

# Using $ADInfo from your code sample
$membersWithfacstaff = $ADInfo.Members | Where-Object {
  ( Get-ADUser $_ -Properties extensionAttribute4 ).extensionAttribute4 -eq 'o365_facstaff'
}

# Use the Count property
$facstaff.Count

正如 cmets 中也提到的,Measure-Command 也会给你计数,但这里有点多余,考虑到如果你想以编程方式使用它,无论如何你都必须引用 Count 属性。


有关在 RSAT AD cmdlet 上有效使用 -Filter 参数的更多信息,请参阅 this answer of mine

【讨论】:

    猜你喜欢
    • 2021-08-15
    • 1970-01-01
    • 2015-10-06
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多