Place forward email id for all users in a file.

Hi,

I have a txt file which has the NT login or email id which ever can be easy to create the script.
A way to forward mails according to the user
Ex:
Sharath@plc.com     Sharath@the.com

Need to set the forward to each user next to it.

I have 300 users for whome i need to forward to different mail id's.

Regards
Sharath
LVL 11
bsharathAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chandru_solCommented:
Can you explain a bit more of what is your needs?

regards
Chandru
0
bsharathAuthor Commented:
I need to forward mails for 200 users to a different account.
Is there a easy way of setting this.
Each user to be forwarded to different mail id's

Like Sharath@plc.com mails to be forwarded to Sharath@the.com and so on....
Ex:
Sharath@plc.com     Sharath@the.com
0
AGCITCommented:
Exchange server? same users names diffrent domains? or completely diffrent people. Recipient polcy to set an alias feasable?
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

chandru_solCommented:
Where to you normally forward the mails in the exchange and which attribute?
0
bsharathAuthor Commented:
Each user has a option to forward a copy of his mail to any internal or external email id.
So that is what i want to do.Each user in the file has to send a mail of his to his individual private email id.Like yahoo or hotmail.
0
chandru_solCommented:
They will br forwarding this or you configure this. If yes where do you configure this?
0
bsharathAuthor Commented:
In users properties > Exchange general > Delivery options > Forward to.

Is the place where i need to assign the new email address.
So need a script which can do this...
0
bsharathAuthor Commented:
Any help on this chandru...
0
RobSampsonCommented:
Sharath, see this tutorial:
http://www.msexchange.org/tutorials/MF015.html

It seems that you need to create the "contact" object for the external person, and then forward the internal person's mail to that external person.

If these external contacts are not already created, then they need to be created.  Then I think we could use the Distinguished Name of that external Contact, and add it to the User's altRecipient or ForwardTo property:

strExternalContactDN = "CN=ExtContact,OU=Contacts,DC=domain,DC=com"

Set objUser = GetObject("LDAP://" & SourceUser)
objUser.Put "AltRecipient", strExternalContactDN
' OR MAYBE ALSO
' objUser.Put "forwardTo", strExternalContactDN
objUser.SetInfo



Regards,

Rob.
0
bsharathAuthor Commented:
Rob i have the contacts already ready.
I have 200 users who always be on travel.Even though the OWA is available for them to access.By chance there is a problem .I need it to be forwarded to some hotmail or yahoo id's
So what i have done is created a Contact and put in this external email id there.Now i just need to put in this email id to the forward tab.
0
bsharathAuthor Commented:
WISH YOU AND YOUR FAMILY A VERY HAPPY NEW YEAR.Rob and Chandru
How was the 31st night?
0
RobSampsonCommented:
OK, so as you already have these contacts, I assume you have tested that assigning an external contact as a forwarding address works? If so, then we can use the email address for a user to find their object, and assign the contact with the email address next to them in the file.  So in your file if you have:
Sharath@plc.com;Sharath@the.com

separate by a semi-colon ( ; ), then the first would be the internal user, the second would be the external user.  Hopefully we would be able to get that to work.

HAPPY NEW YEAR to you as well!  It's 2pm on January 2nd, so I've had some time to recover! LOL!

Regards,

Rob.
0
bsharathAuthor Commented:
Yes Rob i have a file just as listed by you ready with the internal and external email id's
0
RobSampsonCommented:
Sharath, try this script with just one test line in a file called EmailForwards.txt and see if it works:

Where you see this line:
' OR MAYBE ALSO

There is an option above, which is in use now, and an option below, for you to uncomment if the first doesn't work, but don't forget to put a comment on the first one.

Check in the Exchange tab in the AD User Properties to see if it worked, and maybe even send a test email.

'=======================
strInputFile = "EmailForwards.txt"

Const intForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)

While Not objInputFile.AtEndOfStream
      strIntUserADsPath = ""
      strExtUserDN = ""
      arrAddresses = Split(objInputFile.ReadLine, ";")
      strInternalEmail = arrAddresses(0)
      strExternalEmail = arrAddresses(1)
      strIntUserADsPath = Get_LDAP_User_Properties("user", "mail", strInternalEmail, "ADsPath")
      If InStr(strIntUserADsPath, "LDAP://") > 0 Then
            strExtUserADsPath = Get_LDAP_User_Properties("contact", "mail", strExternalEmail, "ADsPath")
            If InStr(strExtUserADsPath, "LDAP://") > 0 Then
                  Set objIntUser = GetObject(strIntUserADsPath)
                  objIntUser.Put "AltRecipient", Replace(strExtUserDN, "LDAP://", "")
                  ' OR MAYBE ALSO
                  'objIntUser.Put "forwardTo", Replace(strExtUserDN, "LDAP://", "")
                  objIntUser.SetInfo
            Else
                  MsgBox "Could not find an external contact with an email address of " & strExternalMail
            End If
      Else
            MsgBox "Could not find an internal user account with an email address of " & strInternalEmail
      End If
Wend

objInputFile.Close
Set objInputFile = Nothing
Set objFSO = Nothing

MsgBox "Finished."

Function Get_LDAP_User_Properties(strObjectType, strSearchField, strObjectToGet, strCommaDelimProps)
     
      ' This is a custom function that connects to the Active Directory, and returns the specific
      ' Active Directory attribute value, of a specific Object.
      ' strObjectType: usually "User" or "Computer"
      ' strSearchField: the field by which to seach the AD by. This acts like an SQL Query's WHERE clause.
      '                        It filters the results by the value of strObjectToGet
      ' strObjectToGet: the value by which the results are filtered by, according the strSearchField.
      '                        For example, if you are searching based on the user account name, strSearchField
      '                        would be "samAccountName", and strObjectToGet would be that speicific account name,
      '                        such as "jsmith".  This equates to "WHERE 'samAccountName' = 'jsmith'"
      '      strCommaDelimProps: the field from the object to actually return.  For example, if you wanted
      '                        the home folder path, as defined by the AD, for a specific user, this would be
      '                        "homeDirectory".  If you want to return the ADsPath so that you can bind to that
      '                        user and get your own parameters from them, then use "ADsPath" as a return string,
      '                        then bind to the user: Set objUser = GetObject("LDAP://" & strReturnADsPath)
     
      ' Now we're checking if the user account passed may have a domain already specified,
      ' in which case we connect to that domain in AD, instead of the default one.
      If InStr(strObjectToGet, "\") > 0 Then
            arrGroupBits = Split(strObjectToGet, "\")
            strDC = arrGroupBits(0)
            strDNSDomain = strDC & "/" & "DC=" & Replace(Mid(strDC, InStr(strDC, ".") + 1), ".", ",DC=")
            strObjectToGet = arrGroupBits(1)
      Else
      ' Otherwise we just connect to the default domain
            Set objRootDSE = GetObject("LDAP://RootDSE")
            strDNSDomain = objRootDSE.Get("DefaultNamingContext")
            'strDNSDomain = objRootDSE.Get("RootDomainNamingContext")
      End If

      strBase = "<LDAP://" & strDNSDomain & ">"
      ' Setup ADO objects.
      Set adoCommand = CreateObject("ADODB.Command")
      Set adoConnection = CreateObject("ADODB.Connection")
      adoConnection.Provider = "ADsDSOObject"
      adoConnection.Open "Active Directory Provider"
      adoCommand.ActiveConnection = adoConnection

 
      ' Filter on user objects.
      'strFilter = "(&(objectCategory=person)(objectClass=user))"
      strFilter = "(&(objectClass=" & strObjectType & ")(" & strSearchField & "=" & strObjectToGet & "))"

      ' Comma delimited list of attribute values to retrieve.
      strAttributes = strCommaDelimProps
      arrProperties = Split(strCommaDelimProps, ",")

      ' Construct the LDAP syntax query.
      strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
      adoCommand.CommandText = strQuery
      ' Define the maximum records to return
      adoCommand.Properties("Page Size") = 100
      adoCommand.Properties("Timeout") = 30
      adoCommand.Properties("Cache Results") = False

      MsgBox strQuery
      ' Run the query.
      Set adoRecordset = adoCommand.Execute
      ' Enumerate the resulting recordset.
      strReturnVal = ""
      Do Until adoRecordset.EOF
          ' Retrieve values and display.    
          For intCount = LBound(arrProperties) To UBound(arrProperties)
                If strReturnVal = "" Then
                      strReturnVal = adoRecordset.Fields(intCount).Value
                Else
                      strReturnVal = strReturnVal & VbCrLf & adoRecordset.Fields(intCount).Value
                End If
          Next
          ' Move to the next record in the recordset.
          adoRecordset.MoveNext
      Loop

      ' Clean up.
      adoRecordset.Close
      adoConnection.Close
      Get_LDAP_User_Properties = strReturnVal

End Function
'=======================

Regards,

Rob.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bsharathAuthor Commented:
Rob i get this...

---------------------------
Windows Script Host
---------------------------
Script:      C:\Set Forwards.vbs
Line:      22
Char:      19
Error:      The specified directory service attribute or value does not exist.
Code:      8007200A
Source:       (null)

---------------------------
OK  
---------------------------

As said i tried like this

strIntUserADsPath = Get_LDAP_User_Properties("user", "mail", strInternalEmail, "ADsPath")
      If InStr(strIntUserADsPath, "LDAP://") > 0 Then
            strExtUserADsPath = Get_LDAP_User_Properties("contact", "mail", strExternalEmail, "ADsPath")
            If InStr(strExtUserADsPath, "LDAP://") > 0 Then
                  Set objIntUser = GetObject(strIntUserADsPath)
                  objIntUser.Put "AltRecipient", Replace(strExtUserDN, "LDAP://", "")
                  ' OR MAYBE ALSO
                  'objIntUser.Put "forwardTo", Replace(strExtUserDN, "LDAP://", "")
                  objIntUser.SetInfo
and then

strIntUserADsPath = Get_LDAP_User_Properties("user", "mail", strInternalEmail, "ADsPath")
      If InStr(strIntUserADsPath, "LDAP://") > 0 Then
            strExtUserADsPath = Get_LDAP_User_Properties("contact", "mail", strExternalEmail, "ADsPath")
            If InStr(strExtUserADsPath, "LDAP://") > 0 Then
                  Set objIntUser = GetObject(strIntUserADsPath)
                 ' objIntUser.Put "AltRecipient", Replace(strExtUserDN, "LDAP://", "")
                  ' OR MAYBE ALSO
                 'objIntUser.Put "forwardTo", Replace(strExtUserDN, "LDAP://", "")
                  objIntUser.SetInfo

Is that corect.For both i get the same error.
0
RobSampsonCommented:
Yes, the first one to try is like this:
                  objIntUser.Put "AltRecipient", Replace(strExtUserDN, "LDAP://", "")
                  ' OR MAYBE ALSO
                  'objIntUser.Put "forwardTo", Replace(strExtUserDN, "LDAP://", "")

and the second like this
                  'objIntUser.Put "AltRecipient", Replace(strExtUserDN, "LDAP://", "")
                  ' OR MAYBE ALSO
                  objIntUser.Put "forwardTo", Replace(strExtUserDN, "LDAP://", "")

One tries the AltRecipient value, the other tries the forwardTo value.....

I don't have exchange so I can't test this....I'll look for another attrbute.....

Rob.
0
bsharathAuthor Commented:
I tried both Rob but get the same error.
0
RobSampsonCommented:
Maybe try
objIntUser.Put "AltRecipient", Replace(strExtUserADsPath, "LDAP://", "")
or
objIntUser.Put "forwardTo", Replace(strExtUserADsPath, "LDAP://", "")
or
objIntUser.Put "AltRecipient", strExtUserADsPath
or
objIntUser.Put "forwardTo", strExtUserADsPath

Regards,

Rob.
0
RobSampsonCommented:
Or maybe
objIntUser.altRecipient = Replace(strExtUserADsPath, "LDAP://", "")

Rob.
0
bsharathAuthor Commented:
If i get a Finish box does it mean that its done and may take some time to reflect?
0
RobSampsonCommented:
If you get no error, then yes, it should have done something with the Forward To property (hopefully). Can you check the User Account in the AD and see if it has been set?

Rob.
0
bsharathAuthor Commented:
Final script that worked great...

'=======================
strInputFile = "EmailForwards.txt"

Const intForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)

While Not objInputFile.AtEndOfStream
      strIntUserADsPath = ""
      strExtUserDN = ""
      arrAddresses = Split(objInputFile.ReadLine, ";")
      strInternalEmail = arrAddresses(0)
      strExternalEmail = arrAddresses(1)
      strIntUserADsPath = Get_LDAP_User_Properties("user", "mail", strInternalEmail, "ADsPath")
      If InStr(strIntUserADsPath, "LDAP://") > 0 Then
            strExtUserADsPath = Get_LDAP_User_Properties("contact", "mail", strExternalEmail, "ADsPath")
            If InStr(strExtUserADsPath, "LDAP://") > 0 Then
                  Set objIntUser = GetObject(strIntUserADsPath)
                   'objIntUser.Put "AltRecipient", Replace(strExtUserDN, "LDAP://", "")
                  ' OR MAYBE ALSO
                  objIntUser.Put "AltRecipient", Replace(strExtUserADsPath, "LDAP://", "")

                 objIntUser.SetInfo
            Else
                  MsgBox "Could not find an external contact with an email address of " & strExternalMail
            End If
      Else
            MsgBox "Could not find an internal user account with an email address of " & strInternalEmail
      End If
Wend

objInputFile.Close
Set objInputFile = Nothing
Set objFSO = Nothing

MsgBox "Finished."

Function Get_LDAP_User_Properties(strObjectType, strSearchField, strObjectToGet, strCommaDelimProps)
     
      ' This is a custom function that connects to the Active Directory, and returns the specific
      ' Active Directory attribute value, of a specific Object.
      ' strObjectType: usually "User" or "Computer"
      ' strSearchField: the field by which to seach the AD by. This acts like an SQL Query's WHERE clause.
      '                        It filters the results by the value of strObjectToGet
      ' strObjectToGet: the value by which the results are filtered by, according the strSearchField.
      '                        For example, if you are searching based on the user account name, strSearchField
      '                        would be "samAccountName", and strObjectToGet would be that speicific account name,
      '                        such as "jsmith".  This equates to "WHERE 'samAccountName' = 'jsmith'"
      '      strCommaDelimProps: the field from the object to actually return.  For example, if you wanted
      '                        the home folder path, as defined by the AD, for a specific user, this would be
      '                        "homeDirectory".  If you want to return the ADsPath so that you can bind to that
      '                        user and get your own parameters from them, then use "ADsPath" as a return string,
      '                        then bind to the user: Set objUser = GetObject("LDAP://" & strReturnADsPath)
     
      ' Now we're checking if the user account passed may have a domain already specified,
      ' in which case we connect to that domain in AD, instead of the default one.
      If InStr(strObjectToGet, "\") > 0 Then
            arrGroupBits = Split(strObjectToGet, "\")
            strDC = arrGroupBits(0)
            strDNSDomain = strDC & "/" & "DC=" & Replace(Mid(strDC, InStr(strDC, ".") + 1), ".", ",DC=")
            strObjectToGet = arrGroupBits(1)
      Else
      ' Otherwise we just connect to the default domain
            Set objRootDSE = GetObject("LDAP://RootDSE")
            strDNSDomain = objRootDSE.Get("DefaultNamingContext")
            'strDNSDomain = objRootDSE.Get("RootDomainNamingContext")
      End If

      strBase = "<LDAP://" & strDNSDomain & ">"
      ' Setup ADO objects.
      Set adoCommand = CreateObject("ADODB.Command")
      Set adoConnection = CreateObject("ADODB.Connection")
      adoConnection.Provider = "ADsDSOObject"
      adoConnection.Open "Active Directory Provider"
      adoCommand.ActiveConnection = adoConnection

 
      ' Filter on user objects.
      'strFilter = "(&(objectCategory=person)(objectClass=user))"
      strFilter = "(&(objectClass=" & strObjectType & ")(" & strSearchField & "=" & strObjectToGet & "))"

      ' Comma delimited list of attribute values to retrieve.
      strAttributes = strCommaDelimProps
      arrProperties = Split(strCommaDelimProps, ",")

      ' Construct the LDAP syntax query.
      strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
      adoCommand.CommandText = strQuery
      ' Define the maximum records to return
      adoCommand.Properties("Page Size") = 100
      adoCommand.Properties("Timeout") = 30
      adoCommand.Properties("Cache Results") = False

      MsgBox strQuery
      ' Run the query.
      Set adoRecordset = adoCommand.Execute
      ' Enumerate the resulting recordset.
      strReturnVal = ""
      Do Until adoRecordset.EOF
          ' Retrieve values and display.    
          For intCount = LBound(arrProperties) To UBound(arrProperties)
                If strReturnVal = "" Then
                      strReturnVal = adoRecordset.Fields(intCount).Value
                Else
                      strReturnVal = strReturnVal & VbCrLf & adoRecordset.Fields(intCount).Value
                End If
          Next
          ' Move to the next record in the recordset.
          adoRecordset.MoveNext
      Loop

      ' Clean up.
      adoRecordset.Close
      adoConnection.Close
      Get_LDAP_User_Properties = strReturnVal

End Function
'=======================



Thanks a lot Rob
0
RobSampsonCommented:
Ah, great, so it was this one that did it:
objIntUser.Put "AltRecipient", Replace(strExtUserADsPath, "LDAP://", "")

Very good.

Regards,

Rob.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Legacy OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.