Info

You are currently browsing the Tech Talk with Homerun Networks weblog archives for the day 10. March 2009.

March 2009
S M T W T F S
« Feb   Apr »
1234567
891011121314
15161718192021
22232425262728
293031  
Categories

Archive for 10. March 2009

Export all email addresses from a domain

Request from Tech Talk reader:  What is the easiest way to export all email addresses out of Active Directory?

Easy, Google the question and there you go.  :-)  Just kidding.  Here is a very basic VBS script you can use.  But first a little background…AD Users & Computers UI lets you list the mail column for each object, which displays the default SMTP email address for objects. You can export the list from ADUC as csv/txt. However, any additional email addresses in the proxyAddresses attribute are not exported.

‘==================================================================================================

‘ VBScript Source File

‘ NAME: LISTEMAILADDRESSES.VBS
‘ VERSION: 1.0
‘ AUTHOR: Myke Reinhold
‘ CREATE DATE  : 7/18/2003
‘ LAST MODIFIED : 10/31/2005
‘==================================================================================================
‘ COMMENT:

‘==================================================================================================

‘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
objCommand.CommandText = _
“<” & strADPath & “>” & “;(&(|(objectClass=contact)(objectClass=group))(mail=*))” & “;distinguishedName,displayName,mail,proxyAddresses;subtree”

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

‘Start procedure

strResult = strResult & VbCrLf & “Domain: ” & strDomain

strResult = strResult & VbCrlf &  “#Total Records Found (other accounts): ” & objRecordSet.RecordCount & VbCrlf
AddressCount = 0

While Not objRecordSet.EOF ‘Iterate through the search results

strUserDN = objRecordSet.Fields(”distinguishedName”)     ‘Get User’s distinguished name from Recordset into a string
set objUser= GetObject(”LDAP://”& strUserDN & “”)         ‘Use string to bind to user object

strResult = strResult & VbCrlf &  “cn: ” & objUser.cn
strResult = strResult & VbCrlf &  “mail: ” & objUser.mail
arrProxyAddresses = objRecordSet.Fields(”proxyAddresses”)
If IsArray(objRecordSet.Fields(”proxyAddresses”)) Then
strResult = strResult & VbCrLf & “Proxy Addresses”

For Each ProxyAddress in arrProxyAddresses

‘Sub: Check X400
If InStr(ProxyAddress, strX400Search) <> 0 Then
‘Wscript.Echo “#This was an x400″
Else
strResult = strResult & VbCrlf &  proxyAddress
End If   ‘Ends loop for X400 address
Next

Else
strResult = strResult & VbCrlf &  “#Object does not have proxy addresses”
End If
strResult = strResult &  VbCrLf

objRecordSet.MoveNext
Wend

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

‘Execute search command to look for user
objCommand.CommandText = _
“<” & strADPath & “>” & “;(&(objectClass=user)(mail=*))” & “;distinguishedName,displayName,mail,proxyAddresses;subtree”

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

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

While Not objRecordSet.EOF ‘Iterate through the search results
strUserDN = objRecordSet.Fields(”distinguishedName”)     ‘Get User’s distinguished name from Recordset into a string
set objUser= GetObject(”LDAP://”& strUserDN & “”)         ‘Use string to bind to user object

If objUser.AccountDisabled = TRUE Then                    ‘If User account disabled, then skip proxy address enum
varDisabledCounter = varDisabledCounter + 1
strResult2 = strResult2 & VbCrLf & varDisabledCounter & ” ” & objUser.displayName & VbCrLf

strResult2 = strResult2 & “cn: ” & objUser.cn
strResult2 = strResult2 & VbCrlf &  “mail: ” & objUser.mail
arrProxyAddresses = objRecordSet.Fields(”proxyAddresses”)
If IsArray(objRecordSet.Fields(”proxyAddresses”)) Then
strResult2 = strResult2 & VbCrLf & “Proxy Addresses”

For Each ProxyAddress in arrProxyAddresses
‘Sub: Check X400
If InStr(ProxyAddress, strX400Search) <> 0 Then
‘Wscript.Echo “#This was an x400″
Else
strResult2 = strResult2 & VbCrlf &  proxyAddress
AddressCount = AddressCount + 1
End If   ‘Ends loop for X400 address
Next
Else
strResult2 = strResult2 & VbCrLf &  “#Object does not have proxy addresses”
End If
strResult2 = strResult2 &  VbCrLf

Else

strResult = strResult & VbCrlf &  “cn: ” & objUser.cn
strResult = strResult & VbCrlf &  “mail: ” & objUser.mail
arrProxyAddresses = objRecordSet.Fields(”proxyAddresses”)
If IsArray(objRecordSet.Fields(”proxyAddresses”)) Then
strResult = strResult & VbCrLf & “Proxy Addresses”

For Each ProxyAddress in arrProxyAddresses
‘Sub: Check X400
If InStr(ProxyAddress, strX400Search) <> 0 Then
‘Wscript.Echo “#This was an x400″
Else
strResult = strResult & VbCrlf &  proxyAddress
AddressCount = AddressCount + 1
End If   ‘Ends loop for X400 address
Next
Else
strResult = strResult & VbCrLf &  “#Object does not have proxy addresses”
End If
strResult = strResult &  VbCrLf

End If   ‘End check for disabled user

objRecordSet.MoveNext
Wend  ‘End second query for users

strResult = “Users, Groups & Contacts” & VbCrLf & “————————-” & VbCrLf & strResult
strResult = strResult & VbCrLf & “Disabled Users” & VbCrLf & “————————-” & VbCrLf & strResult2
WScript.Echo strResult

‘Output to a text file
Set objFileSystem = CreateObject(”Scripting.FileSystemObject”)
Set objOutputFile = objFileSystem.CreateTextFile(”C:\proxyaddresses.txt”)
objOutputFile.Write strResult

This script basically does the following:

  1. Queries Active Directory for Contacts & Groups
  2. Lists their email addresses
  3. Queries Users
  4. Lists enabled users’ email addresses
  5. Lists disabled users’ email addresses separately
  6. Outputs to command line and also to a text file - c:\proxyaddresses.txt

Possible script error - Error list.vbs: (54, 13) (null): 0×80005000

When you look at line 54 it reads:
strUserDN = objRecordSet.Fields(”distinguishedName”)

You have to enter your “distinguishedName” for the script to work.  How how to get it?  It’s fairly simple, Let’s assume your domain is called “example.local”, and the container with employees is called “Employees”.  The distinguishedName in this case wil be “OU=Employees,DC=EXAMPLE,DC=LOCAL”.  If you want to get the name for a subfolder “Employees->Accounting” your name would change to “OU=Accounting,OU=Employees,DC=EXAMPLE,DC=LOCAL”.  Moreover, you can uncomment the following lines (by removing ‘) wscript.Echo strADPath wscript.echo “objDomain: ” & objDomain.distinguishedName to display variables you’re looking for.

|