Question

If I do a query (I plan to use SDS.P) against the global catalog, what should the starting path be so I can search the entire GC?

I want to enumerate all users in GC, for example. Let's say my gc has users for 3 domains (one parent, two children):

TEST.COM
   ONE.TEST.COM
   TWO.TEST.COM

and i'm on a computer in ONE.TEST.COM. I do not want to hardcode DC=XXX,DC=yyy, I would like to determine that at runtime.

TIA! -Will

Was it helpful?

Solution

Here's an example function that queries the global catalog:

class Program
    {

        static void Main()
        {

            DirectoryEntry entry = new DirectoryEntry("GC://dcserver.domain.local",
                                                       "utility",
                                                       "somepassword",
                                                       AuthenticationTypes.Secure );

            const string searchString = "(&(objectCategory=person)(objectClass=user))";

            DirectorySearcher searcher = new DirectorySearcher(entry, 
                                                               searchString, 
                                                               new string[] { "sAMAccountName", "cn" } );

            SearchResultCollection resultCollection = searcher.FindAll( );

            foreach ( SearchResult result in resultCollection )
            {
                Console.WriteLine( result.Path + "\n" + 
                                   result.Properties["cn"][0] + "\n" + 
                                   result.Properties["samaccountname"][0]  );
            }

            Console.ReadLine( );

        }
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top