Solved

VBScript to provide all email addresses in Exchange 2010

Posted on 2013-12-18
6
386 Views
Last Modified: 2014-01-02
I was provided the following script by Experts Exchange, however it does not write a file at all, and when the true is changed to false, nothing different happens.

'==================================================================================================
'
' VBScript Source File
'
' NAME: LISTPROXYADDRESSES.VBS
' VERSION: 0.9.1
' AUTHOR: Bharat Suneja , Nick Phipps
' LAST MODIFIED : 19/04/2011
'==================================================================================================
' COMMENT:
'
'==================================================================================================
'Set up constant for deleting values from multivalued attribute memberOf
option explicit
on error resume next
Const ADS_PROPERTY_NOT_FOUND = &h8000500D
Const ADS_UF_ACCOUNTDISABLE = 2 'For UserAccountControl
Const strX400Search = "X400"
dim strDomain
dim strADPath
dim objDomain
dim strOutput
dim boolTestMode
dim objRootDSE
dim objConnection
dim objCommand
dim objRecordSet
dim strResult
dim AddressCount
dim strUserDN
dim objUser
dim ArrProxyAddresses
dim proxyAddress
'______________________________________________________
'### Change the following line to change the output file path and name
strOutput = ".\email_addresses.txt"
'### Change the following line to "True" to change output to a dialog instead of a TXT file
boolTestMode = false
'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
wscript.Echo "strUserDN: "& strUserDN

set objUser= GetObject("LDAP://"& strUserDN & "") 'Use string to bind to user object
If Err.Number <> 0 Then
Wscript.Echo Computer & " " & Err.Description
Err.Clear
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
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
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

' Wscript.Echo strResult
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 Err.Number <> 0 Then
Wscript.Echo Computer & " " & err.number & " " & Err.Description
Err.Clear
Else

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
end if ' error
objRecordSet.MoveNext
Wend 'End second query for users

strResult = "Users, Groups & Contacts" & VbCrLf & "-------------------------" & VbCrLf & strResult
strResult = strResult & VbCrLf & "Disabled Users" & VbCrLf & "-------------------------" & VbCrLf & strResult2
'Output results
' if boolTestMode then
wscript.echo "strResult contains:"
WScript.Echo strResult
wscript.echo "strResult2 contains:"
wscript.echo strResult2
wscript.echo "End of strResults"

' else
'Output to a text file
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFileSystem.CreateTextFile(strOutput)
objOutputFile.Write strResult
objoutputfile.close
' end if
WScript.Echo "Done."
0
Comment
Question by:100questions
  • 4
  • 2
6 Comments
 
LVL 33

Expert Comment

by:Busbar
ID: 39728648
why the heck you are using this long lousy script, why don't you use powershell:
get-mailbox | Select name,PrimarySmtpAddress | export-csv c:\emailaddresses.csv
0
 

Author Comment

by:100questions
ID: 39728878
I already used PowerShell, I want to use VBScript instead.
0
 
LVL 33

Expert Comment

by:Busbar
ID: 39728939
any specific reason ?!
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 33

Expert Comment

by:Busbar
ID: 39728980
I think you should go with powershell, however here is a script that will work for you:
'==================================================================================================
' 
' VBScript Source File 
' 
' NAME: LISTPROXYADDRESSES.VBS 
' VERSION: 0.9.1 
' AUTHOR: Bharat Suneja , Nick Phipps 
' LAST MODIFIED : 19/04/2011 
'==================================================================================================
' COMMENT: 
' 
'==================================================================================================
'Set up constant for deleting values from multivalued attribute memberOf
option explicit
on error resume next
Const ADS_PROPERTY_NOT_FOUND = &h8000500D 
Const ADS_UF_ACCOUNTDISABLE = 2 'For UserAccountControl 
Const strX400Search = "X400"
dim strDomain 
dim strADPath 
dim objDomain 
dim strOutput 
dim boolTestMode 
dim objRootDSE 
dim objConnection 
dim objCommand 
dim objRecordSet 
dim strResult 
dim AddressCount 
dim strUserDN 
dim objUser 
dim ArrProxyAddresses 
dim proxyAddress
'______________________________________________________
'### Change the following line to change the output file path and name 
strOutput = "C:\Users\administrator.TSXEN\Downloads\email_addresses.txt"
'### Change the following line to "True" to change output to a dialog instead of a TXT file 

'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 
wscript.Echo "strUserDN: "& strUserDN 

set objUser= GetObject("LDAP://"& strUserDN & "") 'Use string to bind to user object 
If Err.Number <> 0 Then 
Wscript.Echo Computer & " " & Err.Description 
Err.Clear 
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 
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 
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 

' Wscript.Echo strResult
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 Err.Number <> 0 Then 
Wscript.Echo Computer & " " & err.number & " " & Err.Description 
Err.Clear 
Else 

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 
end if ' error 
objRecordSet.MoveNext 
Wend 'End second query for users

strResult = "Users, Groups & Contacts" & VbCrLf & "-------------------------" & VbCrLf & strResult 
strResult = strResult & VbCrLf & "Disabled Users" & VbCrLf & "-------------------------" & VbCrLf & strResult2

'Output to a text file 
Dim objFSO 
    Dim objTextFile 
    
    Const ForReading = 1
    Const ForWriting = 2
    Const ForAppending = 8
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.CreateTextFile(".\emails.txt", True)
    
    ' Write a line.
    objTextFile.Write (strResult)

    objTextFile.Close
    'objTextFile.Close

WScript.Echo "Done."

Open in new window

0
 

Author Comment

by:100questions
ID: 39731906
Thanks.  This produces a text file however it's missing most of the email addresses.
It basically shows what used to be displayed with the older script, however I am missing many many users.
0
 
LVL 33

Accepted Solution

by:
Busbar earned 500 total points
ID: 39731983
I just modified the writing portion, didn't remove anything more, so this is the max you can get from the script.

I recommend moving to powershell.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
Know what services you can and cannot, should and should not combine on your server.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now