簡體   English   中英

C#使用SearchResultCollection枚舉大批AD用戶

[英]C# Using SearchResultCollection to enumerate large group of AD users

因此,我要做的是,在一個特定的AD組中有1500多個用戶,當我將其拉低時,我的身份將僅限於獲得誰。 我在MSDN(http://msdn.microsoft.com/zh-cn/library/ms180907%28v=vs.80%29.aspx)上看到了這篇文章,但是它執行了FindOne() ,這樣做使應用程序超過了10個分鍾拉下用戶。 使用ResultsCollection我可以在30秒內打開該應用程序。

何時進行處理

string Last_Name = userResults.Properties["sn"][0].ToString();

它返回錯誤:

索引超出范圍。 必須為非負數且小於集合的大小。\\ r \\ n參數名稱:index“}

我發現這有一個找不到結果的問題,但是, ResultsCollection包含所有1000個條目。 任何幫助表示贊賞。 謝謝!

注意:這些用戶的姓氏不是空的,問題是resultCollection僅返回1個屬性,這就是adpath

DirectoryEntry dEntryhighlevel = new DirectoryEntry("LDAP://OU=Clients,OU=x,DC=h,DC=nt");

DirectorySearcher dSeacher = new DirectorySearcher(dEntryhighlevel);
dSeacher.Filter = "(&(objectClass=user)(memberof=CN=Users,,OU=Clients,OU=x,DC=h,DC=nt))";

uint rangeStep = 1000;
uint rangeLow = 1;
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);
    }

    dSeacher.PropertiesToLoad.Clear();
    dSeacher.PropertiesToLoad.Add(attributeWithRange);

    SearchResultCollection resultCollection = dSeacher.FindAll();

    foreach (SearchResult userResults in resultCollection)
    {
        string Last_Name = userResults.Properties["sn"][0].ToString();
        string First_Name = userResults.Properties["givenname"][0].ToString();
        string userName = userResults.Properties["samAccountName"][0].ToString();
        string Email_Address = userResults.Properties["mail"][0].ToString();
        OriginalList.Add(Last_Name + "|" + First_Name + "|" + userName + "|" + Email_Address);

        if (userResults.Properties.Contains(attributeWithRange))
        {
            foreach (object obj in userResults.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);

看來,如果添加一個PropertiesToLoad,它將不再加載任何其他屬性。 因此,在我的情況下,必須指定要加載的所有屬性。

dSeacher.PropertiesToLoad.Clear();
                dSeacher.PropertiesToLoad.Add(attributeWithRange);
                dSeacher.PropertiesToLoad.Add("givenname");
                dSeacher.PropertiesToLoad.Add("sn");
                dSeacher.PropertiesToLoad.Add("samAccountName");
                dSeacher.PropertiesToLoad.Add("mail");

我不會走這條路。 通過對組的基本搜索來讀取成員資格要容易(且更不容易出錯),而不是像這樣進行大型子樹搜索。

正如您所觀察到的,當您嘗試在一個大型組上讀取member屬性時,每次讀取將僅獲得1500個值。 使小組成員脫身的方法是通過通常稱為“遠程檢索”的功能。 我在此處提供了有關此信息的鏈接: 使用PowerShell始終獲得1500個分發列表的成員

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM