Listing01_List_Domains (VBScript)

This code can be found in Chapter 7 of Managing Enterprise Active Directory Services

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the VBScript code from this site.

'# From the book "Managing Enterprise Active Directory Services"
'# ISBN: 0-672-32125-4

ON ERROR RESUME NEXT
Dim ADsPath, user, passwd, server
server = "dc1"
user   = "administrator@xyz.com"
passwd = "password"

Dim rootDSE
Set rootDSE = GetObject("LDAP://" & strServer & "/RootDSE")
ADsPath =  "<GC://" & server & "/" & _
              rootDSE.Get("rootDomainNamingContext") & ">;"

Dim criteria, properties, scope
criteria = "(ObjectClass=domainDNS);"
properties = "Name;"
scope = "SubTree"

Dim objConnect, objCommand, objRecordSet
Set objConnect = CreateObject("ADODB.Connection")
objConnect.Provider = "ADsDSOObject"
objConnect.Open "Active Directory Provider", user, passwd
Set objCommand = CreateObject("ADODB.Command")
Set objCommand.ActiveConnection = objConnect
'Set the query string
objCommand.CommandText = ADsPath & criteria & properties & scope
'Execute the query
Set objRecordSet = objCommand.Execute
If Err.Number then
    Wscript.Echo "Error occurred during the query: " & Err.Description
    Wscript.Quit
End If

objRecordSet.MoveFirst
While Not objRecordSet.EOF
    Wscript.Echo objRecordSet.Fields(0).Value
    objRecordSet.MoveNext
Wend

This code has been viewed 930 times.

New from the creators of TechTasks.com: StatSheet.com