简体   繁体   中英

How to create DirectoryEntry for specific group

I want to get DirectoryEntry object for eg Domain-Users group. I'm following this link to remove user from a certain group.

strGroup = "CN=TestGroup,OU=TestOU";    
objGroup = objADAM.Children.Find(strGroup, "group");

This code is from MSDN example. If I find my group in Active Directory Users and Computers applet, which is CN and OU?

EDIT: To be more general, where do I find this name for Find method?

From MSDN:

name
Type: System.String
The name of the child directory object for which to search.

Is this the distinguished name or else?

You are mixing two separate environments that share a significant amount of overlap. DirectoryEntry is part of the DirectoryServices package, GetObject is a VBA tool that interfaces with ActiveDs.dll.

If this needs to be visual basic script then you have the equivalent of a DirectoryEntry from GetObject() or the Find() method. If you need access to properties use the object.Get("property_name") method (returns an array for multivalued attributes)

If you can swap development platform to something .NET, you'll have access to all the DirectoryServices you want.

EDIT: If this is a C# .NET app, here's the code to use

private static DirectoryEntry getGroupDE(String group)
{
    String adserver = "dc.companyname.com";
    String searchroot = "ou=Groups,dc=companyname,dc=com";
    DirectorySearcher ds = new DirectorySearcher();
    ds.SearchRoot = new DirectoryEntry(String.Format("LDAP://{0}/{1}",adserver,searchroot));
    ds.SearchScope = SearchScope.Subtree;
    ds.Filter = String.Format("(&(objectCategory=group)(sAMAccountName={0}))",group);
    SearchResult sr = ds.FindOne();
    if (sr == null)
    {
        return null;
    }
    return sr.GetDirectoryEntry();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM