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.
Const ADS_SCOPE_SUBTREE = 2
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
objRecordSet.MoveNext
Loop
Copyright © 2003 - 2012 Lee Derbyshire. All rights reserved.