Link to home
Start Free TrialLog in
Avatar of BNPIT
BNPIT

asked on

VB script to modify Exchange delivery options on specific account

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.
ASKER CERTIFIED SOLUTION
Avatar of prashanthd
prashanthd
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BNPIT
BNPIT

ASKER

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.

 User generated image User generated image
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
Avatar of BNPIT

ASKER

I used double quotes around the email addresses but I still get the error on line 4, char 37.
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

Avatar of BNPIT

ASKER

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?
Avatar of BNPIT

ASKER

Also, it shouldn't be a permissions issue because I have God access to my environment.
Can you try commenting the on error resume next and run the script.

Secondly is it displaying any user DN's to screen?
Avatar of BNPIT

ASKER

This didn't work for us.  The script just threw errors and we gave up after several attempts to modify.