Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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.

↑ Back to the top


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 = "<LDAP://" & varDomainNC & -
 ">;(&(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:
= "<LDAP://" & varDomainNC & -
 ">;(&(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.

↑ Back to the top


References

For more information about how to mailbox-enable a user object programmatically, visit the following MSDN Web site: For more information about how to mail-enable a user object programmatically, visit the following MSDN Web site:

↑ Back to the top


Keywords: KB252650, kbmsg, kbhowto, kbdswadsi2003swept, kbdswadsi2003swept

↑ Back to the top

Article Info
Article ID : 252650
Revision : 5
Created on : 10/25/2006
Published on : 10/25/2006
Exists online : False
Views : 539