Add links to the ActiveDS and ADO type library in your project.
Sub GetDomains ()
Dim objRootDSE As IADs
Dim objBase As IADs
Dim path as string
Dim rsDomains As ADODB.Recordset
Dim cnADS As ADODB.Connection
Dim cmdCommand As ADODB.Command
Set objRootDSE = GetObject ("LDAP: // rootDSE")
path = "LDAP: //" & objRootDSE.Get ("rootDomainNamingContext")
Set objBase = GetObject (path)
Set cnADS = New ADODB.Connection
cnADS.Provider = "ADsDSOObject"
cnADS.Open "ADSI"
Set cmdCommand = New ADODB.Command
cmdCommand.ActiveConnection = cnADS
cmdCommand.Properties ("searchScope") = ADS_SCOPE_SUBTREE
cmdCommand.CommandText = "SELECT Name, distinguishedName FROM '" & objBase.ADsPath & "' WHERE objectCategory = 'domain'"
Set rsDomains = cmdCommand.Execute
Do While rsDomains.EOF = False
List1.AddItem (rsDomains! Name)
rsDomains.MoveNext
Loop
End sub
I have only one domain to check this, so hopefully you will need to tell me if it will receive all domains for you. Also note: I did not add error handling .
source
share