列出所有活动的目录组

27

以下代码列出了一些,但不是全部的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);
    }
}
6个回答

67
如果您使用的是.NET 3.5或更新版本,可以使用PrincipalSearcher和“按示例查询”的主体来进行搜索:
// 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文章在.NET Framework 3.5中管理目录安全主体,一定要阅读,该文章很好地展示了如何充分利用System.DirectoryServices.AccountManagement的新功能。

5
我会将 PrincipalContextGroupPrincipalPrincipalSearcher 封装在 using 块中,因为它们是可处理的。 - David Bentley

2

尝试使用过滤器“(objectcategory=group)” 在这里找到解决方案


抱歉Sergey,结果相同,未列出所有组。Marc_s的答案似乎有效(前提是您使用的是.NET 3.5或更高版本)。 - cymorg
链接已经失效。 - El Asiduo

2
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"
}

这并没有给我正确的结果,但是marc_s的解决方案却做到了。 - JohnB
1
请注意,根据此博客文章中所述,GetDirectoryEntry()将加载所有属性,包括您不需要的属性。 - JohannesB

0

您可以通过以下 PowerShell 获取所有 AD 组的详细信息,如果您想要特定名称的 AD 组,请将 * 替换为筛选器。

Get-ADGroup -Filter * -properties * | Export-csv c:\csv\new.csv


0
要检索超过1000个项目的结果集,您必须将SizeLimit设置为其默认值(零),并将PageSize设置为小于或等于1000的值。
objSearchADAM.PageSize = 1000;

0

我尝试了这个,它有效了

    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);
    }

你好,GetRootDirectoryEntry是什么? - VAAA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接