Link to home
Start Free TrialLog in
Avatar of barrykeel
barrykeel

asked on

Script to get email addresses from Active Directory

I have a script I have modified that is working, but does not write to the text file like I would like.
The results show up as:
DisplayName1
user1@domain.com
DisplayName2
user2@domain.com

I would like for them to show as:
DisplayName1<user1@domain.com>
DisplayName2<user2@domain.com>

Can't seem to get it to work. Any help is appreciated.
'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
 
 
'*************************************
'Begin second query for users
varDisabledCounter = 0                  
 
'Execute search command to look for user
    objCommand.CommandText = _
      "<" & strADPath & ">" & ";(&(objectClass=user)(mail=*))" & ";distinguishedName,displayName,mail;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 & objUser.mail
                       
                 
            Else
            
 
 
                       strResult = strResult & VbCrlf & objUser.cn
                       strResult = strResult & VbCrlf & objUser.mail
    
                      
                
          End If   'End check for disabled user 
            
     objRecordSet.MoveNext 
Wend  'End second query for users
 
 
'Output to a text file
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFileSystem.CreateTextFile("C:\emailaddresses.txt")
objOutputFile.Write strResult

Open in new window

Avatar of Member_2_3684445
Member_2_3684445
Flag of Netherlands image

strResult & VbCrlf & objUser.mail

Remove the  VbCrlf

strResult & objUser.mail & VbCrlf
VbCrlf  Means to insert a \r\n (newline) into the var.

i.e.


set strResult = "abc"
strResult = strResult & VbCrlf & strResult

will output
abc
abc

set strResult = "abc"
strResult = strResult &  strResult

will output
abcabc

Rgrds,
Avatar of barrykeel
barrykeel

ASKER

I have tried removing the VbCrlf and it does not work. I also need to get the < > in the output file.
You need to reformat the string. I.e.
If objUser.AccountDisabled = TRUE Then                  
	varDisabledCounter = varDisabledCounter + 1
	strResult2 = strResult2 & objUser.cn & "<" & objUser.mail & ">" & VbCrlf 
Else
	strResult = strResult & objUser.cn & "<" & objUser.mail & ">" & VbCrlf 
End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of thekod
thekod

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
Avatar of Mike Kline
alternate method is to use something like adfind
 http://www.joeware.net/freetools/tools/adfind/index.htm
adfind -default -f  "&(objectcategory=person)(objectclass=user)(mail=*)" displayname mail -csv -nodn > c:\emailaddresses.csv
Thanks
Mike
Thanks. Worked perfectly.
Shame the accepted solution is a copy of the suggested solution.

In any account, if you want to learn vb scripting you might need to put some effort in learning it.

"Give a man a fish and he will eat for a day, learn a man to fish and he will eat for a lifetime"

Rgrds,
I agree.  Sorry, man, I said so in my post, wasn't trying to steal the points.
Not to caue an issue here, but I tried the suggestion in your post Chris and I could not get it to work. Either there was something with the post or there was something I did incorrectly. BTW, look at the line in the in your oringinal answer that looks like:
If objUser.AccountDisabled = TRUE Then                  
      varDisabledCounter = varDisabledCounter + 1
      strResult2 = strResult2 & objUser.cn & "<" & objUser.mail & ">" & VbCrlf

That is different than the answer I gave points to. With the   "<" & objUser.mail & ">"  in there it did not work for me. Maybe I am overlooking something but it gave me an error.
I also wanted to add that I am learning VB Script and am doing do by trying different scripts, reading, and asking questions. The script I had a problem was a script I got and I heavily modified it to get it to do what I needed. I could not get the results exactly right and then I posed the question here after many attempts with different syntax. Actually my main prolem was the way I was combining the results into a single line and not the VbCrlf. I do know that is a return. Sometimes it just takes a second set of eyes to see what you are overlooking. At least I did not post the question "Can someone give me script to pull email from AD?"