?
Solved

VB script to modify Exchange delivery options on specific account

Posted on 2011-03-11
9
Medium Priority
?
595 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 1000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

777 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