How To Programmatically Get Exchange Server Names From Active Directory

Leave a comment

Ocassionally, someone will ask how you can get a list of Exchange Server names from Active Directory (to avoid having to hard code them into your application). You can do this by looking into the Configuration container of your Active Directory.

Here is some code that you can paste into a .vbs file. It will display your Exchange Server names if you then run it by double-clicking it. You should be able to convert the code into your language of choice.


Set objRoot = GetObject("GC://RootDSE")
strConfig = objRoot.Get("configurationNamingContext")

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
objCommand.CommandText = "SELECT Name FROM 'LDAP://CN=Administrative Groups,CN=First Organization,CN=Microsoft Exchange,CN=Services," & strConfig & "' WHERE objectClass='msExchExchangeServer'"
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute

Do Until objRecordSet.EOF
  WScript.Echo"Server Name: " & objRecordSet.Fields("Name").Value

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>