【问题标题】:Retrieve All Members of Large AD Groups检索大型 AD 组的所有成员
【发布时间】:2014-03-12 14:50:52
【问题描述】:

使用 Microsoft Active Directory 和 Unboundid SDK,并且有一个成员超过 29k 的组。

我正在尝试利用范围值来获取所有组,但无法确定何时到达终点。

我正在使用这种方法:(已更新为工作代码)

  public static List<String> getAttributeRangeBasedSearch(LDAPConnection ldc, String basedn, String filter, int step, String return_attribute) throws LDAPException
{
List<String> allValues = new ArrayList<String>();
// initialize counter to total the group members and range values
int allvalues = 0;
int start = 0;
// int step = 1000;
int finish = step - 1;
boolean finallyFinished = false;
String range;
// loop through the query until we have all the results
while (!finallyFinished)
{
    range = start + "-" + finish;
    String currentRange = return_attribute + ";Range=" + range;
    String range_returnedAtts[] = { currentRange };
    SearchRequest searchRequest = new SearchRequest(basedn, SearchScope.BASE, filter, range_returnedAtts);
    List<SearchResultEntry> rangedEntries = ldc.search(searchRequest).getSearchEntries();
    for (Iterator<SearchResultEntry> iterator = rangedEntries.iterator(); iterator.hasNext();)
    {
    SearchResultEntry searchResultEntry = iterator.next();
    Collection<Attribute> allAttribute = searchResultEntry.getAttributes();
    for (Iterator<Attribute> attributeIterator = allAttribute.iterator(); attributeIterator.hasNext();)
    {
        Attribute attribute = attributeIterator.next();
        log.debug("---> " + allvalues + ": " + attribute.getName());
        if (attribute.getName().endsWith("*"))
        {
        currentRange = attribute.getName();
        finallyFinished = true;
        }
        String[] attributeBatch = searchResultEntry.getAttributeValues(currentRange);
        for (int i = 0; i < attributeBatch.length; i++)
        {
        allValues.add(attributeBatch[i]);
        log.debug("-- " + allvalues++ + " " + attribute.getName() + ":" + attributeBatch[i]);
        }
    }

    }// for SearchResultEntry
    start = start + step;
    finish = finish + step;
}// finallyFinished
return allValues;
}

有什么想法吗?

谢谢 -吉姆

【问题讨论】:

  • AD,默认情况下,每个属性最多只能存储 5000 个条目;如果还有更多属性被分成几个“块” - member0-4999; member5000-9999; 等。你可以将它们合并在一起以获得完整列表吗?或者,您可以进行分页搜索,询问其 memberOf 属性包含您组的可分辨名称的所有用户,但与前一种方法相比,这会慢一些。
  • 问题不在于条目,而是由 MaxValRange 确定的值,对于此实例,每个属性为 1,500。我得到了所有结果,但是当我要求最后一组值时,它是像 member28131-????? 这样的部分大小到一些价值,我不知道是什么。那么如何告诉每个结果中范围的返回值。谢谢-吉姆
  • 其实现在我记得分号正好在属性名称之后,所以只需遍历所有检索到的属性,在分号上拆分它们的名称并将任何同名的属性合并在一起。
  • 所以我得到了它的工作,但这太难懂了,不符合 LDAP 标准。任何人都知道如何使用此处描述的“报告”控件:msdn.microsoft.com/en-us/library/cc223322.aspx?

标签: active-directory ldap unboundid-ldap-sdk


【解决方案1】:

我的工作正常,但这个过程非常困难,目前我正在为该步骤使用硬编码值,因为这可以动态更改从默认值 1,500 到硬编码限制 5,000。

我无法动态确定该值。 Appears, maybe, 如果未在以下位置定义:CN=Query-Policies,CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration,则必须使用默认值,默认值也不同基于所使用的 Microsoft Active Directory 版本。

MSDN 中也有关于 some sort of control 的描述,这可能会有所帮助,但没有关于如何使用它的信息。 有人用过这个吗?

LDAP 策略是使用 lDAPAdminLimits 属性指定的。

queryPolicy 对象的 lDAPAdminLimits 属性是一个多值字符串,其中每个字符串值编码一个名称-值对。在编码中,名称和值用“=”分隔。例如,值为“0”的名称“MaxActiveQueries”的编码是“MaxActiveQueries=0”。每个名称都是 LDAP 策略的名称,值是该策略的值。 AD 林中可以有多个 queryPolicy 对象。 DC根据以下逻辑确定包含其策略的queryPolicy对象:

  • 如果 DC 的 nTDSDSA 对象上存在 queryPolicyObject 属性,DC 将使用它所引用的 queryPolicy 对象。
  • 否则,如果在 DC 所属的 Active Directory 站点的 nTDSSiteSettings 对象上存在 queryPolicyObject 属性,则 DC 使用 Active Directory 站点引用的 queryPolicy 对象。
  • 否则,DC 使用相对于 nTDSService 对象的 DN 为“CN=Default Query Policy,CN=Query-Policies”的 queryPolicy 对象(例如,“CN=Default Query Policy, CN=Query-Policies, CN =Directory Service, CN=Windows NT, CN=Services" 相对于配置 NC 的根目录)。

【讨论】:

  • 这个 DN 是正确的。对于 ADLDS,DN 为 CN=Default Query Policy,CN=Query-Policies,CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration,CN=xxxxxxxxxxx。属性为lDAPAdminLimits,值为MaxValRange=5000。数量可调。
【解决方案2】:

这个可以在文本文件中检索和存储任意数量的用户。此外,如果组为空,它将不会无限循环结束

$myGroup = [string]$args[0];
$myGroup = $myGroup.replace(" ",",");
$group = [adsi]("LDAP://$($myGroup)");
$from = 0 
$all = $false 

$members = @() 


while (! $all) { 
   trap{$script:all = $True;continue} 
   $to = $from + 999 
   $DS = New-Object DirectoryServices.DirectorySearcher($Group,"(objectClass=*)","member;range=$from-$to",'Base') 
   $members += $ds.findall() | foreach {$_.properties | foreach {$_.item($_.PropertyNames -like 'member;*')}} 
   if($from -gt $members.count){
      break;
   }
   $from += 1000 
} 

$currentExecuting = (Get-Item $MyInvocation.MyCommand.Path)
$group.sAMAccountName
$members | measure-object 

$members > "$($currentExecuting.Directory)\$($group.sAMAccountName).txt"

用法:

getADGroupMembers.ps1 CN=groupName,OU=myOrgUnit,DC=contoso,DC=com

【讨论】:

    【解决方案3】:

    这是一个非常好的代码示例,您可以在其中按范围获取组的所有成员。当您也在最后一个范围内时,它也会处理这种情况。您还可以在分页请求中转换此方法。看一看。它帮助了我。

    try
    {
        DirectoryEntry entry = new DirectoryEntry("LDAP://CN=My Distribution List,OU=Distribution Lists,DC=Fabrikam,DC=com");
        DirectorySearcher searcher = new DirectorySearcher(entry);
        searcher.Filter = "(objectClass=*)";
    
        uint rangeStep = 1000;
        uint rangeLow = 0;
        uint rangeHigh = rangeLow + (rangeStep - 1);
        bool lastQuery = false;
        bool quitLoop = false;
    
        do
        {
            string attributeWithRange;
            if(!lastQuery)
            {
                attributeWithRange = String.Format("member;range={0}-{1}", rangeLow, rangeHigh);
        }
            else
            {
                attributeWithRange = String.Format("member;range={0}-*", rangeLow);
        }        
            searcher.PropertiesToLoad.Clear();
            searcher.PropertiesToLoad.Add(attributeWithRange);
            SearchResult results = searcher.FindOne();
            foreach(string res in results.Properties.PropertyNames)
            {
                System.Diagnostics.Debug.WriteLine(res.ToString());
        }
            if(results.Properties.Contains(attributeWithRange))
            {
                foreach(object obj in results.Properties[attributeWithRange])
                {
                    Console.WriteLine(obj.GetType());
                    if(obj.GetType().Equals(typeof(System.String)))
                    {
                }
                    else if (obj.GetType().Equals(typeof(System.Int32)))
                    {
                }
                    Console.WriteLine(obj.ToString());
            }
                if(lastQuery)
                {
                    quitLoop = true;
            }
        }
            else
            {
                lastQuery = true;
        }
            if(!lastQuery)
            {
                rangeLow = rangeHigh + 1;
                rangeHigh = rangeLow + (rangeStep - 1);
        }
    }
        while(!quitLoop);
    }
    catch(Exception ex)
    {
        // Handle exception ex.
    }
    

    来源:http://systemmanager.ru/adam-sdk.en/netds/enumerating_members_in_a_large_group.htm

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-07
      • 2023-03-19
      • 1970-01-01
      • 2022-08-17
      相关资源
      最近更新 更多