Share via


Retrieving a Legacy Exchange Distinguished Name with ADSI

Retrieving a Legacy Exchange Distinguished Name 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

' Retrieving legacy Exchange DN with ADSI
' To maintain compatiblity with previous versions of Exchange server a mail-enabled or mailbox-enabled
' object will store an Exchange distinguished name on that object in Active Directory.  This DN is used for
' replication using the Active Directory Connector (ADC) and should not be confused with the distinguished name for
' that object's location in Active Directory.

' This code can be run from any Windows 2000 or DSClient computer
Sub ExchangeDN()
Dim iAdCont As ActiveDs.IADs
Dim iAdGC As ActiveDs.IADs
Dim Conn As New ADODB.Connection
Dim Com As New ADODB.Command
Dim Rs As ADODB.Recordset
Dim varGCAdsPath As Variant
Dim strQuery As String
Dim strAlias As String


' change to the alias for the mailbox you are looking for
strAlias = "MyAlias"

' Get the Global Catalog
Set iAdCont = GetObject("GC:")
For Each iAdGC In iAdCont
   varGCAdsPath = iAdGC.ADsPath
Next

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

' Build the query to find the user based on his Alias
strQuery = "<" & varGCAdsPath & ">;(mailNickName=" & strAlias & ");name,distinguishedName,legacyExchangeDN;subtree"

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

' Iterate through the results
While Not Rs.EOF
    MsgBox "The mailbox " & Rs.Fields("name") & " has an distinguished name of " & vbLf & _
            Rs.Fields("distinguishedName") & vbLf & " for Active Directory and " & vbLf & _
            Rs.Fields("legacyExchangeDN") & vbLf & " for previous versions of Exchange"
    Rs.MoveNext
Wend

'Clean Up
Rs.Close
Conn.Close
Set Rs = Nothing
Set Com = Nothing
Set Conn = Nothing
Set iAdCont = Nothing
Set iAdGC = Nothing
End Sub

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

Build: June 2007 (2007.618.1)

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