Enumerating Exchange Servers with ADSI

Enumerating Exchange Servers with ADSI

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Visual Basic

' Enumerate Exchange Server computers with ADSI
' The following sample queries Active Directory using ADO for all of the Exchange Server
' computers in the Forest.
' This code can be run from any Windows Server or DSCLient computer.


    Sub main()

    Dim iAdRootDSE As IADs
    Dim Conn As New ADODB.Connection
    Dim Com As New ADODB.Command
    Dim Rs As ADODB.Recordset
    Dim varConfigNC As Variant
    Dim strQuery As String
    Dim varVersion() As Variant

    ' Get the configuration naming context.
    Set iAdRootDSE = GetObject("LDAP://RootDSE")
    varConfigNC = iAdRootDSE.Get("configurationNamingContext")

    ' Open the connection.
    Conn.Provider = "ADsDSOObject"
    Conn.Open "ADs Provider"

    ' Build the query to find all Exchange Server computers.
    strQuery = "<LDAP://" & varConfigNC & ">;(objectCategory=msExchExchangeServer);name,serialNumber;subtree"

    Com.ActiveConnection = Conn
    Com.CommandText = strQuery
    Set Rs = Com.Execute

    ' Iterate through the results.
    While Not Rs.EOF

    ' serialNumber is returned as a variant array.
    varVersion = Rs.Fields("serialNumber").Value

    ' Output the name of the server and the first element of the array.
    MsgBox "Server: " & Rs.Fields("name") & vbLf & "Version: " & varVersion(0)
    Rs.MoveNext
    Wend

    ' Clean up.
    Rs.Close
    Conn.Close
    Set Rs = Nothing
    Set Com = Nothing
    Set Conn = Nothing

End Sub

Send us your feedback about the Microsoft Exchange Server 2003 SDK.

This topic last updated: March 2006

Build: June 2007 (2007.618.1)

© 2003-2006 Microsoft Corporation. All rights reserved. Terms of use.