2010-08-20 80 views
1

我正在嘗試從公司的AD系統中檢索所有電子郵件羣組及其郵件地址。我有大約1800個小組,但是我發現有大約20個傀儡,我無法得到他們的財產。我嘗試了我的觀點,並正確地獲得了郵件地址等屬性。但是我無法通過代碼獲得它們,請有人幫忙。謝謝。以下是我的代碼片段:無法獲取我公司中某些羣組的屬性

static void TestGroupEmails() 
    { 
     ICollection<DirectoryEntry> groups = GetGroups(); 
     Console.WriteLine(groups.Count + "groups"); 
     List<String> noNameGroups = new List<String>(); 
     foreach (DirectoryEntry de in groups) 
     { 
      String name = de.Properties["sAMAccountName"].Value as String; 
      String email = de.Properties["mail"].Value as String; 
      if (String.IsNullOrEmpty(email)) 
       noNameGroups.Add(name); 
     } 
     StreamWriter writer = new StreamWriter(@"C:\ad\group mails.txt"); 
     noNameGroups.Sort(); 
     foreach (String name in noNameGroups) 
     { 
      writer.WriteLine(name); 
     } 
     writer.Close(); 
     Console.ReadLine(); 
    } 

    public static List<DirectoryEntry> GetGroups() 
    { 
     String filter = @"(&(objectCategory=group))"; 

     List<DirectoryEntry> groups = new List<DirectoryEntry>(); 

     using (DirectoryEntry root = new DirectoryEntry(Constants.ADConnPrefix)) 
     { 
      using (DirectorySearcher searcher = new DirectorySearcher(filter, null)) 
      { 
       searcher.PageSize = 10000; 

       searcher.ReferralChasing = ReferralChasingOption.All; 

       searcher.SearchScope = SearchScope.Subtree; 

       searcher.SearchRoot = root; 

       root.Username = Constants.UserName; 
       root.Password = Constants.Password; 

       using (SearchResultCollection searchResult = searcher.FindAll()) 
       { 
        foreach (SearchResult sr in searchResult) 
        { 

         DirectoryEntry de = sr.GetDirectoryEntry(); 
         groups.Add(de); 

        } 
       } 
      } 
     } 
     return groups; 
    } 

    public static SearchResult GetGroupInfo(String groupName) 
    { 
     String normalName = Utility.RemoveLoginNamePrefix(groupName); 
     String filterFormat = "(&(objectCategory=group)(sAMAccountName={0}))"; 
     using (SearchResultCollection searchResult = Search(ADConnPrefix, null, filterFormat, normalName)) 
     { 
      int count = searchResult.Count; 
      SearchResult sr = searchResult[0]; 
      return sr; 
     } 
    } 

回答

1

您確定所討論的羣組確實有電子郵件地址嗎? AD中的團體可能沒有他們。

如果您將搜索過濾器修改爲(&(objectCategory=group)(mail=*)),它將過濾掉沒有電子郵件地址的任何組。

+0

謝謝Bennor。我發現有什麼問題,我應該通過「name」而不是「sAMAccountName」來查詢它們,這就是AD查詢顯示的關於組名的內容。你的過濾器字符串工作,我可以得到我所有的公司電子郵件組,非常感謝 – 2010-08-22 03:11:38