leederbyshire.com
Welcome to www.leederbyshire.com - Mobile MS Exchange web applications.
CONTACT
leederbyshire.com
HOME

OWA FOR PDA

BUY ONLINE

DOWNLOADS

TESTIMONIALS

SCREENSHOTS

FAQ

MANUALS

OTHER UTILITIES

UPGRADE

ARTICLES

How To Programmatically Get Exchange Server Names From Active Directory

Back to Articles page

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.

Microsoft MVP OISV ShareIt! RSS MSExchange.org Reader's choice OWA Addons 1st runner-up