Recipe 6.13. Enumerating Connected Mailboxes in a DatabaseProblemYou want to find out which mailboxes are in a specific mailbox database. SolutionUsing a graphical user interface
Using a command-line interfaceThe following command lists all the mailboxes in a mailbox store: > dsquery * forestroot -scope subtree -filter "(homeMDB=<mailboxStoreDN>") For example: > dsquery * forestroot -scope subtree -filter "(homeMDB=CN=First Storage Group, CN=InformationStore,CN=BATMAN,CN=Servers, CN=First Administrative Group, CN=Administrative Groups,CN=Robichaux and Associates, CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=robichaux,DC=net)" However, this query runs much faster on Windows Server 2003 Active Directory domains, because Windows Server 2003 can do implict indexing of multilinked values. If you're using Windows 2000 Active Directory, you can get the same data (in a slightly different format) with a more efficient query that retrieves it from the linked attribute list on the mailbox store object. This query form looks a little different: > dsquery * <mailboxStoreDN> -scope subtree -attr homeMDBBL This uses the same mailbox DN path as the previous example. Using VBScript' This code finds all of the storage groups on a server. For each SG, ' it lists each mailbox database and the mailboxes contained therein. ' ------ SCRIPT CONFIGURATION ------ strBase = "<LDAP://serverName>;" ' e.g., "batman.robichaux.net" strComputerName = "<serverName>" ' e.g., "batman" ' ------ END CONFIGURATION --------- set theServer = CreateObject("CDOEXM.ExchangeServer") Set theSG = CreateObject("CDOEXM.StorageGroup") Set theConn = CreateObject("ADODB.Connection") theConn.Open "Provider=ADsDSOObject;" theServer.DataSource.Open strComputerName ' examine the SGs; for each SG, iterate through its databases and ' list the mailboxes in it For Each sg In theServer.StorageGroups WScript.Echo "Storage group " & Chr(34) & sg & Chr(34) theSG.DataSource.open sg i = 0 For Each mailDB In theSG.MailboxStoreDBs i = i+1 WScript.Echo " Mailbox database " & i & ": " & mailDB wscript.Echo " Users: " Set objRS = theConn.Execute(strBase & "(homeMDB=" & _ mailDB & "); cn; subtree") objRS.MoveFirst While Not objRS.EOF wscript.echo " " & objRS.Fields(0).Value objRS.MoveNext Wend wscript.echo Chr(13) & Chr(13) Next Next WScript.Echo "Done." DiscussionFinding the mailboxes in a given database is simple, provided you know the DN of the mailbox database itself (which, in turn, is easy to get from the SG object, which is easy to get from the server object) By querying on the homeMDB attribute, you can select the subset of mailboxes in the database of interest. The script for this recipe takes this process up one level by iterating through all of the SGs on the server, then looping through each mailbox database in each SG and displaying the mailboxes that database contains. One caveat, though: a mailbox can exist in Active Directory, but until the user logs onto the mailbox or the mailbox is sent mail, it will not be created in the store. See AlsoRecipe 6.3 for listing the SGs on a server, and Recipes in Chapter 5 for working directly with mailboxes |