Generate a List of all Forwarding Entries in Exchange

Q. How can I generate a list of all forward entries in my Exchange Server system?

A. I recently needed to migrate my email system to another account and discovered that users who had left the company were forwarding email to other people in the company. For the migration, I needed to know all the email addresses for which a person receives mail, including addresses of the people who forward messages to them. I found that the address an account forwards to is stored in the altrecipient attribute, so I wrote the following script to find all users with this attribute populated, then output the users' email addresses and addresses of the mailboxes forwarded to.

' John Savill 10th July 2007
'
Option Explicit

Dim objCommand, objConnection, objChild, objUserConnection, strBase, strFilter, strAttributes
Dim objShell, strRootDomain, strQuery, objRecordset, strName, strforward

' ********************* CHANGE THESE VALUES TO ROOT OF WHERE USERS WILL BE SEARCHED ***********************************

strRootDomain="ou=savilltech,dc=savilltech,dc=net"

' *****************************************************************************************************************************************


Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
strBase = "<LDAP://" & strRootDomain & ">"

strFilter = "(&(objectClass=user)(altRecipient=*))"
strAttributes = "distinguishedName,displayName,mail,altrecipient"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute

' WScript.echo "Running at " & Date()

Do Until objRecordSet.EOF
Set objUserConnection = GetObject("LDAP://" & objRecordSet.Fields("altrecipient").Value)
WScript.echo objRecordSet.Fields("mail").Value & " forward to " & objUserConnection.mail

objRecordSet.MoveNext
Loop

objConnection.Close

To run the script, type the following command line:

C:\Temp>cscript listforwards.vbs

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish