Solved

VB script to modify Exchange delivery options on specific account

Posted on 2011-03-11
9
589 Views
Last Modified: 2012-05-11
Environment:
Windows Server 2003, 2003 native AD, Exchange 2003 SP2

Issue:
At the end of each workday our IT team manually changes the 'Delivery Options' (under 'Exchange General' tab) of our general IT Support AD account to forward to an after-hours email address.  Then first thing in the morning someone has to manually go in and forward it back to a different email address.  The problem is that can occur and important after-hours emails are getting missed.  

Requirement:
We need a way to automate this task (i.e. with VB Script or other script) and run it as a scheduled task.

Unfortunately we don't have in-house scripting experts.  Does anyone out there know how this can be accomplished?

Any help would be appreciated.

Thanks.
0
Comment
Question by:BNPIT
[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
  • 5
  • 4
9 Comments
 
LVL 12

Accepted Solution

by:
prashanthd earned 500 total points
ID: 35115017
Try the following vbscript.

run following from command  cscript filename.vbs

regards
Prashanth


mailboxtomodify = souce@xxxxxxxxxx 'IT Support email address
addresstoforwardto = target@xxxxxxxxxx 'Emailaddress to forward to

Set objmailbox = GetObject("LDAP://"; & getuserdn(mailboxtomodify))
WScript.echo "Forwarding Recipient currently set to : " &
objmailbox.altRecipient
objmailbox.altRecipient = getuserdn(addresstoforwardto)
WScript.echo "Forwarding Recipient changed to : " & objmailbox.altRecipient
objmailbox.deliverAndRedirect = True
objmailbox.setinfo


Function getuserdn(emailaddress)
    
    Set conn = CreateObject("ADODB.Connection")
    Set com = CreateObject("ADODB.Command")
    Set iAdRootDSE = GetObject("LDAP://RootDSE";)
    strNameingContext = iAdRootDSE.Get("defaultNamingContext")
    Conn.Provider = "ADsDSOObject"
    Conn.Open "ADs Provider"
    mbQuery = "<LDAP://"; & strNameingContext & ">;(&(objectclass=user)(mail=" &
    emailaddress & "));name,distinguishedName;subtree"
    Com.ActiveConnection = Conn
    Com.CommandText = mbQuery
    Set Rs = Com.Execute
    While Not Rs.EOF
        Userdn = rs.fields("distinguishedName")
        rs.movenext
    Wend
    getuserdn = userdn
    
End Function

Open in new window

0
 

Author Comment

by:BNPIT
ID: 35130507
Thanks for the quick reply.  I'm getting errors with the script though.

All I modified were the first 2 lines as follows:

mailboxtomodify = itsupport 'IT Support email address
addresstoforwardto = itsupport2 'Emailaddress to forward to

When I ran the script from command prompt (I also tried manually running the vbs file by double-clicking) I got the attached error message.

 command prompt error windows script host error
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35130672
Please give in double quotes

and give the emailaddress

mailboxtomodify = "itsupport@xxx.com" 'IT Support email address
addresstoforwardto = "itsupport2@xxx.com": 'Emailaddress to forward to
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!

 

Author Comment

by:BNPIT
ID: 35130774
I used double quotes around the email addresses but I still get the error on line 4, char 37.
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35134660
Try the folllowing

regards
Prashanth
On Error Resume Next
mailboxtomodify = "" 'IT Support email address
addresstoforwardto = "" 'Emailaddress to forward to

Set objmailbox = GetObject("LDAP://" & getuserdn(mailboxtomodify))
WScript.echo "Forwarding Recipient currently set to : " & objmailbox.altRecipient
objmailbox.altRecipient = getuserdn(addresstoforwardto)
WScript.echo "Forwarding Recipient changed to : " & objmailbox.altRecipient
objmailbox.deliverAndRedirect = True
objmailbox.setinfo

Function getuserdn(emailaddress)
    
    Set conn = CreateObject("ADODB.Connection")
    Set com = CreateObject("ADODB.Command")
    Set iAdRootDSE = GetObject("LDAP://RootDSE")
    strNameingContext = iAdRootDSE.Get("defaultNamingContext")
    Conn.Provider = "ADsDSOObject"
    Conn.Open "ADs Provider"
    mbQuery = "<LDAP://" & strNameingContext & ">;(&(objectclass=user)(mail=" & emailaddress & "));name,distinguishedName;subtree"
    Com.ActiveConnection = Conn
    Com.CommandText = mbQuery
    Set Rs = Com.Execute
    While Not Rs.EOF
        Userdn = rs.fields("distinguishedName")
        WScript.echo userdn
        rs.movenext
    Wend
    getuserdn = userdn
    
End Function

Open in new window

0
 

Author Comment

by:BNPIT
ID: 35139576
Well, I didn't get any errors this time but it didn't change the AD property as expected.  I ran it from a DC, from my Exchange Server, and from a workstation and it never made the change.  Any ideas?
0
 

Author Comment

by:BNPIT
ID: 35140459
Also, it shouldn't be a permissions issue because I have God access to my environment.
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35147335
Can you try commenting the on error resume next and run the script.

Secondly is it displaying any user DN's to screen?
0
 

Author Closing Comment

by:BNPIT
ID: 37389655
This didn't work for us.  The script just threw errors and we gave up after several attempts to modify.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 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