C# 列出所有Active Directory组

C# 列出所有Active Directory组,c#,active-directory,ldap,active-directory-group,C#,Active Directory,Ldap,Active Directory Group,下面的代码列出了一些(但不是全部)Active Directory组。为什么? 我正在尝试列出所有安全组、通讯组、计算机组等。我是否指定了错误的objectClass private static void ListGroups() { DirectoryEntry objADAM = default(DirectoryEntry); DirectoryEntry objGroupEntry = default(DirectoryEntry); DirectorySear

下面的代码列出了一些(但不是全部)Active Directory组。为什么?

我正在尝试列出所有安全组、通讯组、计算机组等。我是否指定了错误的
objectClass

private static void ListGroups()
{
    DirectoryEntry objADAM = default(DirectoryEntry);
    DirectoryEntry objGroupEntry = default(DirectoryEntry);
    DirectorySearcher objSearchADAM = default(DirectorySearcher);
    SearchResultCollection objSearchResults = default(SearchResultCollection);
    SearchResult myResult=null;

    objADAM = new DirectoryEntry(LDAP);
    objADAM.RefreshCache();
    objSearchADAM = new DirectorySearcher(objADAM);
    objSearchADAM.Filter = "(&(objectClass=group))";
    objSearchADAM.SearchScope = SearchScope.Subtree;
    objSearchResults = objSearchADAM.FindAll();

    // Enumerate groups 
    try
    {
        fileGroups.AutoFlush = true;
        if (objSearchResults.Count != 0)
        {
            foreach (SearchResult objResult in objSearchResults)
            {
                myResult = objResult;
                objGroupEntry = objResult.GetDirectoryEntry();
                Console.WriteLine(objGroupEntry.Name);
                fileGroups.WriteLine(objGroupEntry.Name.Substring(3));
            }
        }
        else
        {
            throw new Exception("No groups found");
        }  
    } 
    catch (PrincipalException e)
    {
        fileErrorLog.AutoFlush = true;
        fileErrorLog.WriteLine(e.Message + " " + myResult.Path);
    }
    catch (Exception e)
    {
        throw new Exception(e.Message);
    }
}
尝试筛选“(objectcategory=group)”
找到解决方案

如果您使用的是.NET 3.5或更高版本,则可以使用
原则搜索器
和“示例查询”原则进行搜索:

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// define a "query-by-example" principal - here, we search for a GroupPrincipal 
GroupPrincipal qbeGroup = new GroupPrincipal(ctx);

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);

// find all matches
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
}
如果您还没有-请阅读MSDN文章,这篇文章很好地展示了如何充分利用
System.DirectoryServices.AccountManagement

DirectoryEntry=newdirectoryEntry(“ldap://ldap.gaurangjadia.com“斯科特”,“老虎”);
DirectoryEntry entry = new DirectoryEntry("ldap://ldap.gaurangjadia.com", "scott", "tiger");

DirectorySearcher dSearch = new DirectorySearcher(entry);
dSearch.Filter = "(&(objectClass=group))";
dSearch.SearchScope = SearchScope.Subtree;

SearchResultCollection results = dSearch.FindAll();

for (int i = 0; i < results.Count; i++) {
    DirectoryEntry de = results[i].GetDirectoryEntry();

    //TODO with "de"
}
DirectorySearch dSearch=新的DirectorySearch(条目); dSearch.Filter=“(&(objectClass=group))”; dSearch.SearchScope=SearchScope.Subtree; SearchResultCollection results=dSearch.FindAll(); for(int i=0;i
我试过这个,效果很好

    public ArrayList GetAllGroupNames(string ipAddress, string ouPath)
    {
        DirectorySearcher deSearch = new DirectorySearcher();
        deSearch.SearchRoot = GetRootDirectoryEntry(ipAddress, ouPath);
        deSearch.Filter = "(&(objectClass=group))";
        SearchResultCollection results = deSearch.FindAll();
        if (results.Count > 0)
        {
            ArrayList groupNames = new ArrayList();

            foreach (SearchResult group in results)
            {
                var entry = new DirectoryEntry(group.Path, UserName, Password);
                string shortName = entry.Name.Substring(3, entry.Name.Length - 3);
                groupNames.Add(shortName);
            }

            return groupNames;
        }
        else
        {
            return new ArrayList();
        }
    }

    private DirectoryEntry GetRootDirectoryEntry(string ipAddress, string domainPath, string username, string password)
    {
        var ldapPath = "LDAP://" + ipAddress + "/" + domainPath;
        return new DirectoryEntry(ldapPath, username, password, AuthenticationTypes.Secure);
    }

要检索大于1000项的结果集,必须将SizeLimit设置为其默认值(零),并将PageSize设置为小于或等于1000的值


objSearchADAM.PageSize=1000

您可以通过下面的powershell获取所有广告组的详细信息,如果您需要广告组的特定名称,请编写筛选器而不是*


获取ADGroup-Filter*-properties*|导出csv c:\csv\new.csv

抱歉,Sergey,结果相同,未列出所有组。marc_s的答案似乎有效(前提是您在.NET 3.5或更高版本上)。链接断开了。什么是GetRootDirectoryEntry?我会在使用块时封装
PrincipalContext
GroupPrincipal
PrincipalSearcher
,因为它们是一次性的。这没有给我正确的结果,但是marc_的解决方案做到了。请注意,如中所述,
GetDirectoryEntry()
将加载所有属性,包括您不需要的属性