Share via


Finding the Home Server/Store for a Mailbox with ADSI

Finding the Home Server/Store for a Mailbox 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

' Finding the Home Server/Store for a Mailbox with ADSI
' The following sample will query Active Directory for a given mail user
' and return the Home Server/Storage Group/MDB for that mailbox.
' This code can be run from any Windows 2000 or DSCLient computer.
'
' Visual Basic Project References
'  - Microsoft ActiveX Data Objects 2.5 Library
'  - Active DS Type Library


Sub UserInfo()

Dim iAdRootDSE As ActiveDs.IADs
Dim Conn As New ADODB.Connection
Dim Com As New ADODB.Command
Dim Rs As ADODB.Recordset
Dim varDomainNC As Variant
Dim strQuery As String
Dim strAlias As String
Dim varOwningServerDN As Variant
Dim iAdHomeMDB As ActiveDs.IADs
Dim iAdStorageGroup As ActiveDs.IADs
Dim iAdServer As ActiveDs.IADs

' Change to the alias of the mailbox you are looking for.
strAlias = "user1"

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

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

' Build the query to find the user based on the alias.
strQuery = "<LDAP://" & varDomainNC & ">;(mailNickName=" & strAlias & ");name,homeMDB;subtree"

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

' Iterate through the results.
While Not Rs.EOF

' Retrieve the home MDB for the mailbox.
Set iAdHomeMDB = GetObject("LDAP://" & Rs.Fields("homeMDB"))

' Get the parent of the MDB, which is the storage group.
Set iAdStorageGroup = GetObject(iAdHomeMDB.Parent)

' Find the server that owns the MDB.
varOwningServerDN = iAdHomeMDB.Get("msExchOwningServer")
Set iAdServer = GetObject("LDAP://" & varOwningServerDN)

MsgBox "The mailbox store for " & Rs.Fields("name") & " is located : " & _
vbLf & iAdServer.Get("cn") & "/" & iAdStorageGroup.Get("cn") & _
"/" & iAdHomeMDB.Get("cn")

Set iAdServer = Nothing
Set iAdStorageGroup = Nothing
Set iAdHomeMDB = Nothing

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.

Build: June 2007 (2007.618.1)

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