How to get all windows groups?

本秂侑毒 提交于 2019-12-06 07:46:25

How about setting up a filter for groups and enumerating the results.

Try this filter:

AD.Children.SchemaFilter.Add("group");

Try this one out, it will give you all groups in a specicied OU.

public ArrayList GetGroups()
{
    ArrayList myItems = new ArrayList();

    // Create the principal context for the group object.
    PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ContextOptions.SimpleBind, sServiceUser, sServicePassword);

    // Create the GroupPrincipal object and set the diplay name property. 
    GroupPrincipal oGroupPrincipal = new GroupPrincipal(oPrincipalContext);

    // Create a PrincipalSearcher object.     
    PrincipalSearcher oPrincipalSearcher = new PrincipalSearcher(oGroupPrincipal);

    // Searches for all groups named "Administrators".
    PrincipalSearchResult<Principal> oPrincipalSearchResult = oPrincipalSearcher.FindAll();

    foreach (Principal oResult in oPrincipalSearchResult)
    {
        myItems.Add(oResult.Name);
    }
    return myItems;
}

For a full reference you can check this one out

.Net 3.5 version - > http://anyrest.wordpress.com/2010/06/28/active-directory-c/

Older versions - > http://anyrest.wordpress.com/2010/02/01/active-directory-objects-and-c/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!