Search for a specific group in AD with a part of the CN name

341 views Asked by At

In the AD, there is a list of groups such as:

  • ServerDEV_4283V
  • ServerDEV_4221C
  • ServerDEV_9080M
  • ServerDEV_2722W

....

I am able to get one specific group but I can't get a list of groups with starting with ServerDEV_4.

Here is my code block:

    DirectoryEntry entry = new DirectoryEntry("LDAP://YourServer/OU=SubOU,OU=TopLevelOU,dc=test,dc=com", userName, password,AuthenticationTypes.Secure);

try
{     
     DirectoryEntry childGroup = entry.Children.Find("CN=ServerDEV-4283V");
     // create group here
}
catch (DirectoryServicesCOMException exception)
{
    // handle the "child not found" case here ...
}

Is there a way I could do the RegEx like "ServerDEV-4" so I can get 2 top groups?

Appreciate your help.

Thanks

1

There are 1 answers

0
JPL On

It turned out the wildcard would be enough.

        DirectoryEntry entry = new DirectoryEntry("LDAP://YourServer/OU=SubOU,OU=TopLevelOU,dc=test,dc=com", userName, password,AuthenticationTypes.Secure);

try
{     
     DirectoryEntry childGroup = entry.Children.Find("CN=ServerDEV-4*");
     // create group here
}
catch (DirectoryServicesCOMException exception)
{
    // handle the "child not found" case here ...
}