Active directory win 2003 how do i extract the email addresses of my users ?

We have a domain (2003) and i want to extract all the email addresses in the active directory and put it in a csv or excel file .
How can i do this ?
c_hocklandAsked:
Who is Participating?
 
merowingerCommented:
type in the ip address, usernamer and password

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Set oConn = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
Set oCommand = CreateObject("ADODB.Command")

oConn.Provider = "ADSDSOObject"
oConn.Properties("User ID") = "dc=nt4com,cn=Administrator"
oConn.Properties("Password") = "Password!"

oConn.Open "Active Directory Provider"
Set oCommand.ActiveConnection = oConn
oCommand.CommandText = "<LDAP://192.168.0.200:389>;"& "(&(objectClass=*)(givenName=*));"& "sn,givenName,mail,distinguishedName,member,targetaddress;"& "subTree"
oCommand.Properties("Sort On") = "sn,givenName"
oCommand.Properties("Page Size") = 100
oCommand.Properties("Asynchronous") = True
Set objRecordSet = oCommand.Execute



Do Until objRecordSet.EOF
    count = count + 1
    WScript.echo "Count :" & count
    WScript.echo "SN :" & objRecordSet.Fields("sn")
    WScript.echo "givenName :" & objRecordSet.Fields("givenName")
    WScript.echo "mail :" & objRecordSet.Fields("mail")
    WScript.echo "member:" & objRecordSet.Fields("member")
    WScript.echo "distinguishedName:" & objRecordSet.Fields("distinguishedName")
    objRecordSet.MoveNext ' Next User
Loop
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
 
c_hocklandAuthor Commented:
ok great , but..how do i run this ?? not quite familliar with VB scripts.
0
 
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
Here's a script that will write to a tab separated file (c:\UserEmailAddr.txt) all the users in AD. You can open it in Excel. It has the fields User, Email, and DN.

The script below can be run as is from any computer or account in the domain (unless there's read restrictions that have been imposed)

HTH

'===========================================
'* Enter the DN of the container you want to base your
'* search in, excluding the domain name (dc=domain,dc=com),
'* between the double quotes below. If you want it to
'* search the root leave it blank.

strContainer = "" 


strDirectory = "c:\"
strFile = "UserEmailAddr.txt"
strOutput = strDirectory & strFile


'------ Check or Create csv file -------

' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
   Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "Just created " & strDirectory
End If

If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
Wscript.Echo "Just created " & strDirectory & strFile
End If

set objFolder = nothing
set objFile = nothing

If err.number = vbEmpty then
Set objShell = CreateObject("WScript.Shell")
objShell.run ("Explorer" & " " & strDirectory)
Else WScript.echo "VBScript Error: " & err.number
End If


'---------- Write to file -----------

Set objRootDSE = GetObject("LDAP://rootDSE")

If strContainer = "" Then
    strADsPath = objRootDSE.Get("defaultNamingContext")
Else
    strADsPath = strContainer & "," & objRootDSE.Get("defaultNamingContext")
End If

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

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


'--- change the DN to where you want to start your search ---
objCommand.CommandText = _
    "<LDAP://" & strADsPath & ">;" & _
        "(objectCategory=User)" & _
        ";distinguishedName,name,mail;subtree"
 
Set objRecordSet = objCommand.Execute
set fso = createobject("scripting.filesystemobject")

Set objFile = FSO.OpenTextFile(strOutput, 8)
objFile.WriteLine "Username" & vbTab & "Email Address" & vbTab & "Distinguished Name"
objFile.Close

While Not objRecordSet.EOF

    Set objFile = FSO.OpenTextFile(strOutput, 8)
    objFile.WriteLine objRecordSet.Fields("Name") & vbTab & _
                      objRecordSet.Fields("Mail") & vbTab & _
                      objRecordSet.Fields("distinguishedName")
    objFile.Close
    objRecordSet.MoveNext

Wend

objConnection.Close
'===============================================
0
 
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
BTW, if you're wanting to edit the script you can ignore
"'--- change the DN to where you want to start your search ---"
I left that comment there by mistake. (it's from an older version)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.