Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Place forward email id for all users in a file.

Posted on 2007-12-02
23
Medium Priority
?
381 Views
Last Modified: 2008-06-01
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
0
Comment
Question by:bsharath
[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
  • 11
  • 8
  • 3
  • +1
23 Comments
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20394716
Can you explain a bit more of what is your needs?

regards
Chandru
0
 
LVL 11

Author Comment

by:bsharath
ID: 20394730
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
 
LVL 4

Expert Comment

by:AGCIT
ID: 20400268
Exchange server? same users names diffrent domains? or completely diffrent people. Recipient polcy to set an alias feasable?
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 12

Expert Comment

by:chandru_sol
ID: 20433472
Where to you normally forward the mails in the exchange and which attribute?
0
 
LVL 11

Author Comment

by:bsharath
ID: 20434019
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
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20434632
They will br forwarding this or you configure this. If yes where do you configure this?
0
 
LVL 11

Author Comment

by:bsharath
ID: 20436236
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
 
LVL 11

Author Comment

by:bsharath
ID: 20489971
Any help on this chandru...
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20562317
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
 
LVL 11

Author Comment

by:bsharath
ID: 20562321
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
 
LVL 11

Author Comment

by:bsharath
ID: 20562331
WISH YOU AND YOUR FAMILY A VERY HAPPY NEW YEAR.Rob and Chandru
How was the 31st night?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20562369
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
 
LVL 11

Author Comment

by:bsharath
ID: 20562375
Yes Rob i have a file just as listed by you ready with the internal and external email id's
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 20569939
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
 
LVL 11

Author Comment

by:bsharath
ID: 20570016
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 20570047
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
 
LVL 11

Author Comment

by:bsharath
ID: 20570059
I tried both Rob but get the same error.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20570914
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 20570922
Or maybe
objIntUser.altRecipient = Replace(strExtUserADsPath, "LDAP://", "")

Rob.
0
 
LVL 11

Author Comment

by:bsharath
ID: 20570930
If i get a Finish box does it mean that its done and may take some time to reflect?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20570938
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
 
LVL 11

Author Comment

by:bsharath
ID: 20570949
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 20570972
Ah, great, so it was this one that did it:
objIntUser.Put "AltRecipient", Replace(strExtUserADsPath, "LDAP://", "")

Very good.

Regards,

Rob.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

715 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