Solved

VB script to modify Exchange delivery options on specific account

Posted on 2011-03-11
9
574 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
  • 5
  • 4
9 Comments
 
LVL 12

Accepted Solution

by:
prashanthd earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:BNPIT
Comment Utility
I used double quotes around the email addresses but I still get the error on line 4, char 37.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 12

Expert Comment

by:prashanthd
Comment Utility
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
Comment Utility
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
Comment Utility
Also, it shouldn't be a permissions issue because I have God access to my environment.
0
 
LVL 12

Expert Comment

by:prashanthd
Comment Utility
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
Comment Utility
This didn't work for us.  The script just threw errors and we gave up after several attempts to modify.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

15 Experts available now in Live!

Get 1:1 Help Now