none
Exchange 2003 - need script to set forwarding on users account. RRS feed

  • Question

  • Hello,

    I need to modify a few hundred user accounts and forward/redirect their mail to another address. Does anyone have an example of a script to do this please for Exchange 2003?

    Thanks,

    - Alan.

     

     

    Thursday, December 9, 2010 7:16 PM

Answers

  •  You need to modify the altRecipient property of the user to a distinguishedName of an AD object that your forwarding to (eg a contact etc) and the deliverAndRedirect property controls if a message is forwardard only or forwardard and delivered. An example using VBS look like

    mailboxtomodify = EMAIL REMOVED
    addresstoforwardto = EMAIL REMOVED
    
    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=person)(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 
    

    Cheers
    Glen

    • Marked as answer by AMGT Friday, December 10, 2010 3:54 PM
    Friday, December 10, 2010 1:56 AM

All replies

  •  You need to modify the altRecipient property of the user to a distinguishedName of an AD object that your forwarding to (eg a contact etc) and the deliverAndRedirect property controls if a message is forwardard only or forwardard and delivered. An example using VBS look like

    mailboxtomodify = EMAIL REMOVED
    addresstoforwardto = EMAIL REMOVED
    
    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=person)(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 
    

    Cheers
    Glen

    • Marked as answer by AMGT Friday, December 10, 2010 3:54 PM
    Friday, December 10, 2010 1:56 AM
  • Thanks again Glen.
    Friday, December 10, 2010 3:55 PM