How can I list all domain controllers (DCs) for an Active Directory (AD) domain?

John Savill

April 22, 2006

1 Min Read
ITPro Today logo in a gray background | ITPro Today

A. The following script, which you can download here , accepts a domain as its parameter, then lists all DCs in that domain. Some lines wrap because of space constraints.

Const ADS_SCOPE_SUBTREE = 2

If Wscript.Arguments.Count required. For example:" & vbCrLf _ & "cscript listdcs.vbs dc=savilltech,dc=com" Wscript.Quit(0) End If

strDomainName = Wscript.Arguments(0)

Set objConnection = CreateObject("ADODB.Connection") Set objCommand = CreateObject("ADODB.Command") objConnection.Provider = "ADsDSOObject" objConnection.Open "Active Directory Provider" Set objCOmmand.ActiveConnection = objConnection

objCommand.CommandText = _ "Select distinguishedName from " & _ "'LDAP://cn=Configuration," & strDomainName & "' " _ & "where objectClass='nTDSDSA'" objCommand.Properties("Page Size") = 1000 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

Set objRecordSet = objCommand.Execute objRecordSet.MoveFirst

Do Until objRecordSet.EOF DCDN = objRecordSet.Fields("distinguishedName").Value DomainController = Mid(DCDN,21,InStr(Mid(DCDN,21),",")-1) Wscript.Echo "Domain Controller: " & DomainController objRecordSet.MoveNext Loop

This sample execution produced the following output when I ran it on my system:

D:Temp>cscript listdcs.vbs dc=savilltech,dc=com

Domain Controller: SAVDALDC01
Domain Controller: SAVDALDC02
Domain Controller: SAVLONDC01
Domain Controller: SAVDALLABDC01

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like