Link to home
Start Free TrialLog in
Avatar of Joe_27
Joe_27

asked on

Export/Extract All SMTP Address from Windows 2003 Active Directory

Hi Experts,

I have a Windows 2003 Domain, in addition to having Exchange 2003 Enterprise.  I have been tasked with gathering all email addresses within the company.  To accomplish this task, I used the CSVDE –f <filename> command.  When I opened the file, everything seemed to be in order; however, the proxy address column had additional information.  Not only did it pull the SMTP addresses, but it also pulled the X400, CCMail, and MS fields as well.  

Does anyone have a script or know of anything that just extracts only information contained within SMTP addresses?  I would hate to have to go through rows and rows of an Excel worksheet, deleting the unnecessary information.

Thanks for your help.
Avatar of flyguybob
flyguybob
Flag of United States of America image

You can always add the column in the display within Exchange System Manager and then export the list.

Bob
Avatar of Chris Dent

Retrieving all the e-mail addresses is really easy (when you know how of course). Do you need anything more than the e-mail addresses? The account they're associated with for example?

If you just want a plain list then this will extract them for you and dump them to Addresses.txt (save as <something>.vbs and run it):

Option Explicit

' Global Constants

Const ADS_SCOPE_SUBTREE = 2

' Global Variables

Dim objFileSystem, objFile, objRootDSE
Dim strDomainName

'
' Subroutines
'

Sub FindAddress

      ' Runs a simple AD Query to find proxyAddresses.

      Dim objConnection, objCommand, objRecordSet
      Dim strAddress
      Dim arrAddresses

      Set objConnection = CreateObject("ADODB.Connection")
      objConnection.Provider = "ADsDSOObject"
      objConnection.Open "Active Directory Provider"

      Set objCommand = CreateObject("ADODB.Command")
      objCommand.ActiveConnection = objConnection

      objCommand.CommandText = "SELECT proxyAddresses FROM 'LDAP://" & strDomainName & "'"

      objCommand.Properties("Page Size") = 1000
      objCommand.Properties("Timeout") = 600
      objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
      objCommand.Properties("Cache Results") = False

      Set objRecordSet = objCommand.Execute

      While Not objRecordSet.EOF
            On Error Resume Next
            arrAddresses = objRecordSet.Fields("proxyAddresses")
            For Each strAddress in arrAddresses
                  If InStr(1, strAddress, "smtp:", VbTextCompare) Then
                        objFile.WriteLine Replace(LCase(strAddress), "smtp:", "")
                  End If
            Next
            On Error Goto 0
            objRecordSet.MoveNext
      Wend

      objConnection.Close

      Set objRecordSet = Nothing
      Set objCommand = Nothing
      Set objConnection = Nothing
End Sub

'
' Main Code
'

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objFile = objFileSystem.OpenTextFile("Addresses.txt", 2, True, 0)

Set objRootDSE = GetObject("LDAP://RootDSE")
strDomainName = objRootDSE.Get("defaultNamingContext")
WScript.Echo "Searching Domain: " & strDomainName & VbCrLf

FindAddress

Set objFile = Nothing
Set objFileSystem = Nothing

Oh anad the script should be run as Administrator... the SQL queries are a little fussy about access rights.

Chris
ASKER CERTIFIED SOLUTION
Avatar of Ludovick Lagrevol
Ludovick Lagrevol
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nice VBScript!
Avatar of fishtechbdw
fishtechbdw

Hi ingetic - your idea works great for users but I can't work out how to use it to get group email addresses.

Hi Chris-Dent - your idea works well and being a program I'm sure someone can tell me how I could modify it to also get the Display Name and Type for each object returned, inlcuding users and groups.

Thanks

Barry
Avatar of Joe_27

ASKER

Yes, the display name and type would be great info to have also.  

Very easy to add - how do you want it to be displayed?
Avatar of Joe_27

ASKER

If it's possible, Display Name, Type, smtp addresses
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
for group, you can use :
dsquery group
Hi Ingetic

dsquery group works but then it won't let me do dsget group -email. Is there a way of extracting the email addresses for groups like that?

I'll give Chris-Dent's suggestion a go when I'm back in the office.

Thanks to both of you for the ideas - I posted my first question on the Outlook section (about linked addresses in distribution lists) a couple of weeks ago and got zero response, which surprised me. This thread just shows how well Experts Exchange works with everyone contributing.

Regards

Barry
I didn't ask the question but I did join in and get some useful help so I would recommend splitting the points with the larger share to Chris_Dent for his vbscript and ingetic for the command line idea.

Just my 2 cents worth.

Thanks guys for the help

Barry