Active Directory Query issue

M

mpriem

Hi,

I am trying to enumerate Exchange Admin groups, but fail to with the
folowing code. Can someone help me with this issue.
The executing user has sufficient permissions.

Code:
using System;

namespace Active_Directory
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
System.DirectoryServices.DirectoryEntry entry =
new
System.DirectoryServices.DirectoryEntry(@"LDAP://DC=<rootdomain>");

System.DirectoryServices.DirectorySearcher mySearcher = new
System.DirectoryServices.DirectorySearcher(entry);
mySearcher.Filter =
"(&(objectClass=msExchAdminGroup)(objectCategory=ms-Exch-Admin-Group))";
Console.WriteLine(mySearcher.FindAll().Count);
}
}
}
 
D

Denis Dougall

You need to iterate through the result collection..

akin to

mySearcher.Filter =
"(&(objectClass=msExchAdminGroup)(objectCategory=ms-Exch-Admin-Group))";
foreach(System.DirectoryServices.SearchResult result in
mySearcher.FindAll())
{
Console.WriteLine( result.GetDirectoryEntry().Path );
}


Denis
 
M

Marc Scheuner [MVP ADSI]

I am trying to enumerate Exchange Admin groups, but fail to with the
folowing code. Can someone help me with this issue.
DirectoryEntry entry = new DirectoryEntry(@"LDAP://DC=<rootdomain>");

Can you spell out this LDAP path completely? What exactly is it? If
the DirEntry that you start off from is wrong, your search will never
work. You should try to use the "defaultNamingContext" from RootDSE
for your search, unless you have a clear reason NOT to use that as a
starting point.

--------------------------------
DirectoryEntry deRoot = new DirectoryEntry("LDAP://RootDSE");

string sDefaultNamingContext =
deRoot.Properties["defaultNamingContext"].Value.ToString();

DirectoryEntry entry = new DirectoryEntry("LDAP://" +
sDefaultNamingContext);

--------------------------------

Also, I've never successfully used the .Count property on the
collection returned by FindAll() - I'd just iterate over all the
entries to verify that you get anything back.

Marc
================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
 
M

mpriem

Thanks Guys,

It works now. Even the .Count property. I thinks I messed up the DN.

Regards,

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top