Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scripting forwarding mail to an external contact in Exchange 3

Status
Not open for further replies.

gmiles

Programmer
Jun 14, 2003
31
0
0
US
Hi all,
We have a subset of users who we need to modify their account settings so as to forward their e-mail to another address for the time being. I know that this is done in Exchange by creating a new mail contact and then enabling mail forwarding. However, I don't really want to do this manually for all the users concerned.
Is there an easy way to script this? Thanks
-gmiles
 
We have Exchange 2003. In Active Directory Users and Computers, you can highlight every user account and click on Exchange Tasks - then click on Create Email Address.

Then you can enter the email address into each user object of where the email should go.

Check out to find a VBS script that you can use to modify user objects in Active Directory.
 
That's different than what gmiles wants. He needs to forward to a mail enabled contact.

gmiles - quite coincidental, really. I'm waiting for a Michigan company to do the same for one of my clients.

You can use
dsadd contact .......
to create the actual contacts.

In theory, you could use dsmod to modify the actual user accounts to set the forwarding (I'm not near a machine to verify that dsmod lets you change the forwarding) to the contacts you've created.

Pat Richard, MCSE MCSA:Messaging CNA
Microsoft Exchange MVP
Want to know how email works? Read for yourself -
 
After a good nights sleep and re-reading...sounds like your right. I would still check out the Script Guys site because they have a lot of reference scripts that do a ton of stuff. If not, and you know VBS and ASDI - you should be able to write a script to do what you need.
 
I saw a couple of hackable scripts on today that would probably do what you need.

One to create contacts, one to activate the forwarding.

Neill
 
Sorry for the delay in joining in guys.

Here is the code you need. You can easily alter this to read a list from Excel to swap out the user name and contact information as needed.

Code:
'==========================================================================
'
' NAME: SetForwardToInfo.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 5/17/2007
' COPYRIGHT (c) 2007 All Rights Reserved
'
' COMMENT: Forwards email to an AD contact.
'
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED To
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'    PARTICULAR PURPOSE.
'
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS 
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
'    OF THIS CODE OR INFORMATION.
'
'==========================================================================

SourceUser = SearchDistinguishedName("UserLoginName") 'The user login name
ForwardTo = SearchContact("Jack Contact")' The contact common name


Set objUser = GetObject _
  ("LDAP://" & SourceUser)
 
objUser.Put "AltRecipient", ForwardTo
objUser.SetInfo



Public Function SearchDistinguishedName(ByVal vSAN)
    Dim oRootDSE, oConnection, oCommand, oRecordSet

    Set oRootDSE = GetObject("LDAP://rootDSE")
    Set oConnection = CreateObject("ADODB.Connection")
    oConnection.Open "Provider=ADsDSOObject;"
    Set oCommand = CreateObject("ADODB.Command")
    oCommand.ActiveConnection = oConnection
    oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & _
        ">;(&(objectCategory=User)(samAccountName=" & vSAN & "));distinguishedName;subtree"
    Set oRecordSet = oCommand.Execute
    On Error Resume Next
    SearchDistinguishedName = oRecordSet.Fields("DistinguishedName")
    On Error GoTo 0
    oConnection.Close
    Set oRecordSet = Nothing
    Set oCommand = Nothing
    Set oConnection = Nothing
    Set oRootDSE = Nothing
End Function

Public Function SearchContact(ByVal vSAN)
    Dim oRootDSE, oConnection, oCommand, oRecordSet

    Set oRootDSE = GetObject("LDAP://rootDSE")
    Set oConnection = CreateObject("ADODB.Connection")
    oConnection.Open "Provider=ADsDSOObject;"
    Set oCommand = CreateObject("ADODB.Command")
    oCommand.ActiveConnection = oConnection
    oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & _
        ">;(&(objectCategory=Contact)(cn=" & vSAN & "));distinguishedName;subtree"
    Set oRecordSet = oCommand.Execute
    On Error Resume Next
    SearchContact = oRecordSet.Fields("DistinguishedName")
    On Error GoTo 0
    oConnection.Close
    Set oRecordSet = Nothing
    Set oCommand = Nothing
    Set oConnection = Nothing
    Set oRootDSE = Nothing
End Function

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
This was my second script to do this task. The first one would indeed create an Active Directory contact, and forward mail to the contact, but it wasn't an /Exchange/ AD contact, so mail would bounce.
Word to the wise for people like me: AD contacts aren't quite the same as Exchange AD contacts. You'll need the latter if you're going to be sending e-mail to them.

So, what this script does is take input from the command line on what the user's account name is and what their forwarding address should be. Then it creates the contact, and finally, sets that contact as the forwarding address for that person. It mostly comes from here, with some modification:

For us, the OU was forwarding, and you'll want to change the rest of the information to work for your domain.

Option Explicit


Dim objRootLDAP, objContainer, objContact, objExcel, objSheet
Dim strOU, strContactName, strPathExcel, strEmail, strProxy
Dim intRow, strYourDescription, strFirst, strLast, strMainDefault
Dim strMailbox, strNick, SourceUser, ForwardTo, objUser

SourceUser = WScript.Arguments(0) 'The user login name input from command line
ForwardTo = WScript.Arguments(1) 'The contact e-mail input from command line

' Part 1: Set string variables
strOU = "ou=Forwarding ,"
strYourDescription = SourceUser & "'s Contact"
strMainDefault = "SMTP:" & ForwardTo
strContactName= SourceUser & "_Forward"
strFirst = SourceUser
strLast= "Forwarding address"
strProxy = ForwardTo
strEmail = ForwardTo
strMailbox = "/o= ORGANIZATION /ou=Admin Group/cn=Recipients/cn=" & SourceUser & "_forward"
strNick = strContactName

'Part 2: Building the contact
' Section to bind to Active Directory
Set objContainer = GetObject("LDAP://" & strOU & "ou=Contacts,dc=YOURDOMAIN,dc=COM")

' Build the actual contact.
Set objContact = objContainer.Create("Contact",_
"cn=" & strContactName)
objContact.Put "Mail", strEmail
objContact.Put "givenName", strFirst
objContact.Put "sn", strLast
objContact.Put "proxyAddresses", strProxy
objContact.Put "targetAddress", strMainDefault
objContact.Put "legacyExchangeDN", strMailbox
objContact.Put "mailNickname", strNick
objContact.SetInfo

'Part 3: Set the contact as the forward address
Set objUser = GetObject("LDAP://cn=" & SourceUser & ",cn=Users,dc=YOURDOMAIN,dc=COM")
objUser.Put "AltRecipient", "cn="& SourceUser & "_forward,OU=Forwarding,OU=Contacts,DC=YOURDOMAIN,DC=COM"
objUser.SetInfo

WScript.Quit
 
Awesome scripts guys. One more for the markdmac script folder, and I might need to create another folder for you gmiles!
 
Glad it helped someone else out.

I wouldn't create a folder for me just yet... I only code when I can't get someone else to do it... And even then, it isn't coding per se, it's just ve-e-ry cautious cutting and pasting... ;-)

Well, maybe a -little- coding.
 
pointlaugh.gif


Oh that really made me laugh. Thanks Pat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top