Microsoft KB Archive/252650

= How To Find Mail/Mailbox-Enabled Users in the Active Directory =

Article ID: 252650

Article Last Modified on 10/25/2006

-

APPLIES TO


 * Microsoft Active Directory Service Interfaces 2.5
 * Microsoft Active Directory Service Interfaces 2.5
 * Microsoft Exchange 2000 Server Standard Edition

-



This article was previously published under Q252650



SUMMARY
The purpose of the Microsoft Visual Basic code sample that is provided in this article is to demonstrate how to use Active Directory Services Interfaces and ActiveX Data Objects (ADO) to query a Windows 2000 domain for mail-enabled user objects and mailbox-enabled user objects.



MORE INFORMATION
Mail-enabled user objects are users that have an external mailbox. Mailbox-enabled user objects are users that have an Exchange 2000 mailbox.

The following sample code queries for mail-enabled and mailbox-enabled user objects by setting the query to include: (&(objectClass=user)(mail=*))

Sample Code
Sub Main 'the purpose of this sample is to demonstrate how to query for 'mail/mailbox enabled User objects '

Private Sub Command1_Click Dim oRootDSE As IADs Dim objUser As IADsUser Dim oConnection As New ADODB.Connection Dim oCommand As New ADODB.Command Dim RS As ADODB.Recordset Dim strQuery As String, newattrib As String Dim varDomainNC As Variant

On Error Resume Next ' Get the Configuration Naming Context Set oRootDSE = GetObject("LDAP://RootDSE") varDomainNC = oRootDSE.Get("defaultNamingContext") ' Open the Connection oConnection.Provider = "ADsDSOObject" oConnection.Open "ADs Provider" 'Build a query to find all mail enabled Users that do not have 'extensionattribute2 set strQuery = ";(&(objectClass=user)(mail=*));adspath,cn;subtree" oCommand.ActiveConnection = oConnection oCommand.CommandText = strQuery Set RS = oCommand.Execute If RS.RecordCount = 0 Then Debug.Print "No mailbox enabled User found to not have extension attribute 2 set" Else While Not RS.EOF Set objUser = GetObject(RS.Fields("adspath")) Debug.Print objUser.cn, " does not have extension attribe2 set" RS.MoveNext Wend End If Set oRootDSE = Nothing Set objUser = Nothing RS.Close set RS = Nothing End Sub Another common query is to find all the mail-enabled/mailbox-enabled users who have a specific attribute set. The following sample query finds all the users who do not have a value set for the extension2 attribute. If you add (!(extensionAttribute2=*)) to the query shown earlier, you would specify that the resulting recordset should contain objects that do not have the extension2 attribute set. The query would look like this: = ";(&(objectClass=user)(mail=*)(!(extensionAttribute2=*)));adspath,cn;subtree"

Note You can view the extension2 attribute on a user object by using the Active Directory Users and Computers Microsoft Management Console (MMC) snap-in. To do so, view the Properties of a mail-enabled/mailbox-enabled user object. To find the extension2 attribute, view the Properties of the user object, click the Exchange Advanced tab, and then click Custom Attributes.

