Submitted By: Anonymous Submission

Lists all the domain controllers in current domain.

Visual Basic
Edit|Remove
' Search all DC's in current domain
Dim arrDClist()   ' List of Domain Controllers is stored in arrDClist array

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.Properties("Sort On") = "name"
Set objRootDSE = GetObject("LDAP://rootDSE")   ' automatically discover current domain
strADsPath = "LDAP://OU=Domain Controllers," & objRootDSE.Get("defaultNamingContext")
objCommand.CommandText = _
   "<" & strADsPath & ">;(&(objectCategory=computer)" & _
   "(cn=*));" & "distinguishedName,name;subtree"
Set objRecordSet = objCommand.Execute   ' Execute query to search for Domain Controllers

i = 0   ' Store DC names in array
While Not objRecordSet.EOF
   ReDim Preserve arrDClist(i) 
   arrDClist(i) = objRecordSet.Fields("name")
   WScript.Echo arrDClist(i)
   objRecordSet.MoveNext
   i = i +1
Wend