【问题标题】:How to get all groups that a user is a member of, based on group extensionattribute如何根据组扩展属性获取用户所属的所有组
【发布时间】:2020-12-11 06:58:01
【问题描述】:

我正在尝试获取域用户所属的所有组,但仅过滤具有给定 extensionattribute 的组。 我设置了所有域组的 extensionattribute12 以更好地过滤一些查询(即基础设施 - 安全 - 电子学习)。我的查询应该只获取具有

的用户组

extensionattribute12=安全

(例如)。 我使用类似的东西:

get-aduser -filter  -Properties memberof | select name, @{ l="GroupMembership"; e={$_.memberof  -join ";"  } }

我得到了所有用户组。如何按组扩展属性过滤?

【问题讨论】:

  • 您只需要直接组成员身份还是应该解决间接组成员身份?
  • 嗨。你的意思是嵌套组?不,只有直接组成员身份。

标签: powershell active-directory ldap


【解决方案1】:

您可以使用反向关系(组对象上的member)来查询用户所属的所有组,每个用户只需 1 个查询。这里使用 LDAP 过滤器:

$groupLabel = "Security"

Get-ADUser -Filter * |ForEach-Object {
  $groups = Get-ADGroup -LDAPFilter "(&(extensionattribute12=$groupLabel)(member=$($_.DistinguishedName)))"

  [pscustomobject]@{
    User = $_.SamAccountName
    GroupMembership = $groups.DistinguishedName -join ';'
  }
}

如果您必须处理大量用户或组成员身份,您可能会发现预先检索所有满足 extensionAttribute12 条件的组并使用该列表过滤用户的 memberOf 属性会更快:

$groupLabel = "Security"
# Create a hash set and populate it with the distinguished 
# names of all the groups we're looking for
$groupDNs = [System.Collections.Generic.HashSet[string]]::new(@(
  Get-ADGroup -Filter "extensionAttribute12 -eq '$groupLabel'" |Select -Expand DistinguishedName
))

Get-ADUser -Filter * -Properties memberOf |ForEach-Object {
  # Retrieve memberOf values and filter against the hash set
  $groups = $_.memberOf |Where-Object { $groupDNs.Contains($_) }

  [pscustomobject]@{
    User = $_.SamAccountName
    GroupMembership = $groups -join ';'
  }
}

【讨论】:

    【解决方案2】:

    用 N+1 个查询来实现

    $groups = @( Get-ADGroup -Filter '(extensionattribute12 -eq "security")' )
    $users = @( $groups | 
        ForEach-Object { Get-ADGroupMember -Identity $_ -Recursive } | 
        Sort-Object -Unique )
    
    $users # All users of all groups that have EA12 = security
    

    【讨论】:

      【解决方案3】:
      Get-ADUser -filter {...} -Properties memberof | select name, @{ l="GroupMembership"; e={( $_.memberof | Get-ADGroup |?{ $_.extensionattribute12 -eq 'security' }) -join ";" }} |?{ $_.GroupMembership }
      

      【讨论】:

      • 干得好,如果你添加一些关于你的代码的上下文会更好
      猜你喜欢
      • 2011-07-01
      • 1970-01-01
      • 2023-03-21
      • 2012-06-09
      • 2020-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多