Someone asked on delphifeeds.com, how to get a list of all domains within an environment. Since I was going to need just the same soon for a application in development right now, I thought I take up the challenge and try my luck. After using Google I found nil solutions using Delphi, so I went to microsoft.com and looked how they do it, using VBScript.
Well, I was lucky and found a solution, but as so often, getting it to work on Delphi is an entirely different task. First you have to find the root naming context of the domain where your application is running. You use the ADs library for that task. From that LDAP-path you replace the prefix with GC, which stands for global catalog.
// get the forest base try DirObj := VBGetObject('LDAP://rootDSE') as IADs; Root := (VBGetObject('LDAP://' + DirObj.Get('rootDomainNamingContext')) as IADs).ADsPath; except mmoReport.Lines.Add('Computer is not running in domain setup.'); Exit; end; // replace LDAP with GC (global catalog) Root := 'GC' + Copy(Root, 5, MaxInt); mmoReport.Lines.Add(Root);
Next, you query the AD using ADO for the domains or sites, whatever you want. The queries are simple:
Query := 'SELECT Name FROM ' + QuotedStr(Root) + ' WHERE objectCategory=''domain'''; Query := 'SELECT Name FROM ' + QuotedStr(Root) + ' WHERE objectCategory=''site''';
Next, you just iterate the record set, returned after executing your command. A full demo can be downloaded (ZIP, 34 Kb) from my site.
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
Theme design by Jelle Druyts