Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBScript to provide all email addresses in Exchange 2010

Posted on 2013-12-18
6
Medium Priority
?
411 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
Know the reasons and solutions to move/import EDB to New Exchange Server. Also, find out how to recover an Exchange .edb file and to restore the file back.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

670 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