List All Exchange Email Addresses

January 19, 2010

Here’s a real time saver for you Exchange admins out there that need a quick list of all the email account on the domain…

This script I picked up and modified from the ExchangepediaBlog does the trick!

<p><font face="Courier New">'==================================================================================================
' VBScript Source File
' NAME: LISTEMAIL.VBS
' VERSION: 1.0
' BY: Dave Abad at Logical Alternative
'     Modified From Bharat Suneja's ExchangePediaBlog Post LISTPROXYADDRESSES.VBS
'==================================================================================================
'Set up constant for deleting values from multivalued attribute memberOf
Const ADS_PROPERTY_NOT_FOUND  = &h8000500D
Const ADS_UF_ACCOUNTDISABLE = 2 'For UserAccountControl
Const strX400Search = "X400"
'______________________________________________________

'Set RootDSE
Set objRootDSE = GetObject("LDAP://rootDSE")
strDomain = objRootDSE.Get("defaultNamingContext")
strADPath = "LDAP://" & strDomain
'wscript.Echo strADPath
Set objDomain = GetObject(strADPath)
'wscript.echo "objDomain: " & objDomain.distinguishedName

'Setup ADODB connection
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

'Execute search command to look for Contacts & Groups
sCT = "<" & strADPath & ">;(&(|(objectClass=contact)(objectClass=group))(mail=*))"
sCT = sCT & ";distinguishedName,displayName,mail,proxyAddresses;subtree"
objCommand.CommandText = sCT

'Execute search to get Recordset
Set objRecordSet = objCommand.Execute

'Start procedure
strResult = strResult & VbCrLf & "Domain: " & strDomain
strResult = strResult & VbCrlf &  "#Total Records Found (other accounts): "
strResult = strResult & objRecordSet.RecordCount & VbCrlf
AddressCount = 0

While Not objRecordSet.EOF 'Iterate through the search results
 'Get User's distinguished name from Recordset into a string
 strUserDN = objRecordSet.Fields("distinguishedName")
 'Use string to bind to user object
 set objUser= GetObject("LDAP://"& strUserDN & "")
 strResult = strResult & objUser.mail & VbCrLf
 objRecordSet.MoveNext
Wend

'*************************************
'Begin second query for users
varDisabledCounter = 0                

'Execute search command to look for user
sCT = "<" & strADPath & ">;(&(objectClass=user)(mail=*))"
sCT = sCT & ";distinguishedName,displayName,mail,proxyAddresses;subtree"
objCommand.CommandText =  sCT

'Execute search to get Recordset
Set objRecordSet = objCommand.Execute

strResult = strResult & vbCrlf &  "#Users"
strResult = strResult & VbCrlf &  "#Total Records Found (users): "
strResult = strResult & objRecordSet.RecordCount & VbCrlf

While Not objRecordSet.EOF 'Iterate through the search results
 'Get User's distinguished name from Recordset into a string
 strUserDN = objRecordSet.Fields("distinguishedName")
 'Use string to bind to user object
 set objUser= GetObject("LDAP://"& strUserDN & "")
 If objUser.AccountDisabled = TRUE Then
 varDisabledCounter = varDisabledCounter + 1
 strResult2 = strResult2 & objUser.mail & VbCrLf
 Else
 strResult = strResult & objUser.mail & VbCrLf
 End If
 objRecordSet.MoveNext
Wend

strOut = "Users, Groups & Contacts" & VbCrLf
strOut = strOut & "-------------------------" & VbCrLf & strResult
strOut = strOut & VbCrLf & "Disabled Users"
strOut = strOut & VbCrLf & "-------------------------" & VbCrLf & strResult2
'WScript.Echo strResult

'Output to a text file
aDomain = split(strDomain,"=")
aDomain2 = split(aDomain(1),",")
strDomain = aDomain2(0)
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFileSystem.CreateTextFile(".\" & strDomain & "_" & "email_list.txt")
objOutputFile.Write strOut</font></p>
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.