【问题标题】:Return list of ad accounts and check if member of group返回广告帐户列表并检查是否为组成员
【发布时间】:2021-04-25 19:18:18
【问题描述】:

我已经编写了以下内容,并且它可以工作(基于 Check if the user is a member of a list of AD groups),但是它需要 难以置信 很长时间才能运行 - 我假设这是因为它检索了整个组对于每个用户。我已经尝试在开始时将 $members... 行移出函数以检索一次组列表,但似乎没有任何区别。

有没有更有效的方式返回这些信息?

samaccountname   enabled   InDenyGroup 
--------------   -------   ----------- 
admin-abc        True      yes         
admin-def        True      yes         

在此示例中,帐户名称过滤器为“king”,因为检查帐户是否在组中。

Get-ADUser -Filter "(SamAccountName -like 'admin*') -and (enabled -eq 'true')" | 
    ft -AutoSize samaccountname,enabled,@{Name='InBlockGroup'; Expression={InDenyGrp($_.samaccountname)}}


Function InDenyGrp([string]$UserID) {
    $members = Get-ADGroupMember -Identity "myBlockGroup" | Select -ExpandProperty SamAccountName

    If ($members -contains $UserID) {
        Return "yes"
    } Else {
        Return "not in group"
    }
}

谢谢。

【问题讨论】:

    标签: powershell active-directory


    【解决方案1】:

    您在 Foreach-Object 循环的每次迭代中一次又一次地查询同一 ADGroup 的所有 ADGroup 成员(不仅是 DistinguishedNames)(这就是瓶颈)。

    您要么只是查询“blockGroup”的成员(请参阅您发布的链接)并遍历成员并检查您的用户是否属于他们(有一些属性可以与之比较),或者您尝试下面的代码:

    构建查找表应该会提高性能。 此外,我们不需要比 DistinguishedNames 更多的关于组成员的信息,因此 Get-ADGroupMember 是多余的。

    您可以使用不同组的成员扩展 LookupTable。

    # query blocking group with it's members first (only DistinguishedNames)
    $adGroup = Get-ADGroup -Identity '<myBlockGroup>' -Properties 'Members'
    
    # build lookup table of members' DistinguishedNames 
    $adGroupMemberLookupTable = [System.Collections.Generic.HashSet[string]]::new()
    foreach ($member in $adGroup.Members) {
        [void]$adGroupMemberLookupTable.Add($member)
    }
    
    Get-ADUser -Filter "(SamAccountName -like 'admin*') -and (enabled -eq 'true')" | 
        Format-Table -AutoSize samaccountname, enabled, 
        @{Name ='InBlockGroup'; 
            Expression = { 
                # lookup if user is member of a "blocking" group
                $adGroupMemberLookupTable.Contains($_.DistinguishedName) 
            } 
        }
    

    【讨论】:

    • 我同意,就在上周,我写了一些与此非常相似的东西。几乎相同的概念。
    猜你喜欢
    • 1970-01-01
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多