Solved

Sample vbscript to set Forwarding to external contacts.

Posted on 2010-09-17
60
1,780 Views
Last Modified: 2012-05-10
I'm using Exchange 2003, and I'm looking for vbscript of sorts that will enable me to configure about 100 users to forward to various contacts as well as set to send to local mailbox. Below is a sample script I found. However, I need to place these users in a spreadsheet. How do I configure the spreadsheet and use it in a script? Any thoughts?

Thanks much!!


So if Bill Gates wanted to set his email to be forwarded to Steve Ballmer
and also to his mailbox a very simple VBScript to do this might look like
this:

Set objUser = GetObject ("LDAP://CN=Bill Gates,CN=Users,DC=microsoft,DC=com")
objUser.Put "altRecipient", "CN=Steve Ballmer,CN=Users,DC=microsoft,DC=com"
objUser.Put "DeliverandRedirect", True
objUser.SetInfo

You could write a slightly more complex script to set these attributes for
all users in a domain or all users in an OU.

0
Comment
Question by:kt KTAdmin
  • 33
  • 27
60 Comments
 
LVL 32

Expert Comment

by:endital1097
ID: 33703269
the following assumes your csv has the user DN in the first column and the forward in column 2

on error resume next
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("C:\users.txt", ForReading)
Do While ObjTextFile.AtEndOfStream <> True
    strNextLine = objTextFile.Readline
   arrayUser = Split(strNextLine, ",")
Set objUser = GetObject ("LDAP://" & arrayUser(0))
objUser.Put "altRecipient", arrayUser(1)
objUser.Put "DeliverandRedirect", True
objUser.SetInfo

    Loop
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33703304
you can validate the script by just running the following

on error resume next
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("C:\users.txt", ForReading)
Do While ObjTextFile.AtEndOfStream <> True
    strNextLine = objTextFile.Readline
   arrayUser = Split(strNextLine, ",")
WScript.Echo arrayUser(0)
WScript.Echo arrayUser(1)
    Loop
0
 

Author Comment

by:kt KTAdmin
ID: 33703319
Thanks. I will give it a try!!
0
 

Author Comment

by:kt KTAdmin
ID: 33703482
Sage,

Would my columns be similar to this:

cn=smith, joe,ou=Contacts,ou=test,dc=local,dc=edu (Would this be the DN or  "altRecipient" column?)
"deliverAndRedirect" column is TRUE

and would the OpenTextFile work on a csv or xls?


0
 
LVL 32

Expert Comment

by:endital1097
ID: 33703547
yes, that would be good for the altRecipientColumn

won't work as csv
remove the header columns and rename as txt

you don't need the deliverandredirect if they are all true

how are you determining the mailbox to add this alt recipient
0
 

Author Comment

by:kt KTAdmin
ID: 33703578
I did a query to find those w/o forwarding addresses. Also, what would the DN column look like?  Or what would it look like as txt file?
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33703596
text file containing all the DNs
0
 

Author Comment

by:kt KTAdmin
ID: 33703608
OK
0
 

Author Comment

by:kt KTAdmin
ID: 33703616
So I need to include the altRecipient value since that will be included in the script. Correct?
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33703636
what is the overall goal, because right now the script is just going to change a single user with the last value in the text file
0
 

Author Comment

by:kt KTAdmin
ID: 33703764
The goal is to add a forwarding external email address and also keep a local copy. These users will have different forwarding email addresses. I already have the contact records I want to use. I have already identified the users.
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33703787
you need to have the user dn and the forwarding dn
then use the script
0
 

Author Comment

by:kt KTAdmin
ID: 33703836
Please explain. Are these two separate files? Also, I'm using Exchange 2003 sp2.
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33703903
no, have the dn of the mailbox , dn of the forwarding address

example: john smith should have all messages forwarded to jane doe

cn=john smith,cn=jane doe (abbreviated)
0
 

Author Comment

by:kt KTAdmin
ID: 33704579
the scripts runs with no errors, however the forwarding with the contact shown does not show in the user properties. Here is the text file

"CN=allen\\, Melanie,OU=Enrolled,OU=CAU Students,DC=cau,DC=edu cn=Allen. Melanie,OU=Contacts,OU=Cau Students,DC=cau,DC=edu"                                                
Any thoughts?                                                                                    
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33704634
i don't know why i didn't think of it
don't use a comma to separate the two name
instead use a "|" character
then replace the following line

arrayUser = Split(strNextLine, "|")

the DN contains multiple commas, which breaks the arrayUser
0
 

Author Comment

by:kt KTAdmin
ID: 33704784
So here is what I have:

Option Explicit
Dim objFSO, objTextfile, objUser

on error resume next
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("C:\contact.txt", ForReading)
Do While ObjTextFile.AtEndOfStream <> True
    strNextLine = objTextFile.Readline
   arrayUser = Split(strNextLine, "|")
Set objUser = GetObject ("LDAP://" & arrayUser(0))
objUser.Put "altRecipient", arrayUser(1)
objUser.Put "DeliverandRedirect", True
objUser.SetInfo

    Loop

WScript.Echo " Task done!"

WScript.Sleep 5000    'Sleep for five seconds

WScript.Quit


Again, the script runs w/o errors but no forwarding shown in users property
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33704822
take out the following lines and run again
the on error resume next will let it complete even with errors
option explicit without arrayuser would make the arrayuser varialble invalid

Option Explicit
on error resume next
0
 

Author Comment

by:kt KTAdmin
ID: 33704856
It errors on line 11 "Server not operational"

this line 11:   Set objUser = GetObject ("LDAP://" & arrayUser(0))
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33704879
put the following in front of that line to verify the value

WScript.Echo arrayUser(0)
0
 

Author Comment

by:kt KTAdmin
ID: 33704920
It verifies the contact.txt contents which is "CN=allen\\, Melanie,OU=Enrolled,OU=CAU Students,DC=cau,DC=edu cn=Allen. Melanie,OU=Contacts,OU=Cau Students,DC=cau,DC=edu"      

But i still get Server not operational. Line 12
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33704934
that is not valid
it should only be
CN=allen\\, Melanie,OU=Enrolled,OU=CAU Students,DC=cau,DC=edu

this is the alt recipient
cn=Allen. Melanie,OU=Contacts,OU=Cau Students,DC=cau,DC=edu

can you post a line from the text file
0
 

Author Comment

by:kt KTAdmin
ID: 33704969
"CN=allen\\, Melanie,OU=Enrolled,OU=CAU Students,DC=cau,DC=edu"
"cn=Allen. Melanie,OU=Contacts,OU=Cau Students,DC=cau,DC=edu"

this is the text lines. Again, I get "server not operational"-- Should I set the following as such:

Set objRootLDAP = GetObject("LDAP://rootDSE")
Set objContainer = GetObject("LDAP://" & strOU _
& objRootLDAP.Get("DefaultNamingContext"))

0
 

Author Comment

by:kt KTAdmin
ID: 33704979
Also this

strOU = "OU=Enrolled,OU=Cau Students
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33704990
the value should only be "CN=allen\\, Melanie,OU=Enrolled,OU=CAU Students,DC=cau,DC=edu" for arrayUser(0)

arrayUser(1) should equal "cn=Allen. Melanie,OU=Contacts,OU=Cau Students,DC=cau,DC=edu"

please post the text line as is
0
 

Author Comment

by:kt KTAdmin
ID: 33705016
I'm sorry. I don't quite understand. What would the script look like afterwards? The text file as well?
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33705024
the text file should look like this
CN=allen\\, Melanie,OU=Enrolled,OU=CAU Students,DC=cau,DC=edu|cn=Allen. Melanie,OU=Contacts,OU=Cau Students,DC=cau,DC=edu
0
 

Author Comment

by:kt KTAdmin
ID: 33705072
OK. I get a different error on line 13 which is:  Set objUser = GetObject ("LDAP://" & arrayUser(0))

Again, I place the ( | ) pipe symbol between edu and cn in the Text file.
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33705085
you don't have the "" around the values anymore either correct?

once again run with the following to validate the value
Wscript.echo arrayuser(0)
0
 

Author Comment

by:kt KTAdmin
ID: 33705086
the Wscript.Echo shows only CN=allen\\, Melanie,OU=Enrolled,OU=CAU Students,DC=cau,DC=edu
0
Are end users causing IT problems again?

You’ve taken the time to design and update all your end user’s email signatures, only to find out they’re messing up the HTML, changing the font and ruining the imagery. What can you do to prevent this? Find out how you can save your signatures from end users today.

 
LVL 32

Expert Comment

by:endital1097
ID: 33705124
change the following
arrayUser = Split(strNextLine, "|")
Set objUser = GetObject ("LDAP://" & arrayUser(0))

to be this
arrayUser = Split(strNextLine, "|")
mailbox = "LDAP://" & arrayUser(0)
wscript.echo mailbox
Set objUser = GetObject (mailbox)
0
 

Author Comment

by:kt KTAdmin
ID: 33705181
here is what I have and it still errors out:

Dim objFSO, objTextfile, objUser

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("C:\contact.txt", ForReading)
Do While ObjTextFile.AtEndOfStream <> True
    strNextLine = objTextFile.Readline
   arrayUser = Split(strNextLine, "|")

Mailbox = "LDAP://" & arrayUser(0)
WScript.Echo Mailbox
Set objectUser = GetObject (mailbox)

objUser.Put "altRecipient", arrayUser(1)
objUser.Put "DeliverandRedirect", True
objUser.SetInfo

    Loop
0
 

Author Comment

by:kt KTAdmin
ID: 33705203
it errors on line 15 which is Set objectUser = GetObject (mailbox)

the Wscript.Echo shows: LDAP://"CN=allen\\, Melanie,OU=Enrolled...
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33705225
why are there " in there
are they in the text file
0
 

Author Comment

by:kt KTAdmin
ID: 33705234
Yes, since there are spaces in the line therefore I include " " Not correct?
0
 

Author Comment

by:kt KTAdmin
ID: 33705247
I get "invalid DN syntax" w/o the " "
0
 

Author Comment

by:kt KTAdmin
ID: 33705265
Will this script pull the allen. melanie contact record from the Contacts OU and place it in the forward email section of the allen, melanie user account? The contact email is not the same as the user account emai.
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33705268
proper syntax does not include them
it should be
LDAP://CN=user,ou=orgunit,dc=domain,dc=com

i wonder if it has to do with the dn containing \\
0
 

Author Comment

by:kt KTAdmin
ID: 33705308
Again, I truly thank for your help and patience!! I'll continue tomorow.
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33705945
i'm going to create an account like yours in my lab and make it work
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33705975
verified it is the DN format

LDAP://CN=Administrator,CN=users,DC=fabrikam,DC=com
CN=Administrator
LDAP://CN=User,\\ Test,CN=users,DC=fabrikam,DC=com
C:\Documents and Settings\Administrator\My Documents\test.vbs(13, 1) (null): An
invalid dn syntax has been specified.
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33706026
it looks like the format of your users is wrong

it should be
CN=allen\, Melanie,OU=Enrolled,OU=CAU Students,DC=cau,DC=edu|cn=Allen
0
 

Author Comment

by:kt KTAdmin
ID: 33706172
It errors out on line 15:    Set objectUser = GetObject (mailbox)

"CN=allen\, Melanie,OU=Enrolled,OU=CAU Students,DC=cau,DC=edu|cn=Allen. Melanie,OU=Contacts,OU=Cau Students,DC=cau,DC=edu"                                                                                                                                                                  
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33706187
run the following
ldifde -f c:\export.txt -d "OU=Enrolled,OU=CAU Students,DC=cau,DC=edu"

then search the export.txt file for allen
make sure that the dn value in export.txt matches what is in hte file used by this script

post the value so i can see if there are any special characters we need to address
0
 

Author Comment

by:kt KTAdmin
ID: 33706214
I used csvde and ran an export. this is the DN field: CN=Baldwin\\, Carmella,OU=Enrolled,OU=CAU Students,DC=cau,DC=edu

This is under the altRepient field: CN=Baldwin. Carmella Denise,OU=Contacts,OU=CAU Students,DC=cau,DC=edu

should I still use ldifde?
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33706227
no, that is fine
what is strange is the use of double \\
there should only be one as an escape character

try updating this section
Mailbox = "LDAP://" & arrayUser(0)

with
Mailbox = Replace(Mailbox, "\\," "\\\,")
Mailbox = "LDAP://" & arrayUser(0)

the extra \ is an escape character for the extra \ in the DN
0
 

Author Comment

by:kt KTAdmin
ID: 33706250
It errors line 34:  Mailbox = Replace(Mailbox, "\\," "\\\,")
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33706258
sorry missed a comma
Mailbox = Replace(Mailbox, "\\,","\\\,")
0
 

Author Comment

by:kt KTAdmin
ID: 33706341
errors line 17, 1st character:  Set objectUser = GetObject (mailbox)
0
 

Author Comment

by:kt KTAdmin
ID: 33706360
Here is the full scipt

Dim objFSO, objTextfile, objUser

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("C:\contact.txt", ForReading)
Do While ObjTextFile.AtEndOfStream <> True
    strNextLine = objTextFile.Readline
   arrayUser = Split(strNextLine, "|")


Mailbox = Replace(Mailbox, "\\,","\\\,")
Mailbox = "LDAP://" & arrayUser(0)

WScript.Echo Mailbox
Set objectUser = GetObject (mailbox)

objUser.Put "altRecipient", arrayUser(1)
objUser.Put "DeliverandRedirect", True
objUser.SetInfo

    Loop

WScript.Echo " Task done!"
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33706386
here's what i have working in the lab
the only difference is i don't have the extra \ so i don't have the replace line


Dim objFSO, objTextfile, objUser

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("C:\contacts.txt", ForReading)
Do While ObjTextFile.AtEndOfStream <> True
    strNextLine = objTextFile.Readline
   arrayUser = Split(strNextLine, "|")

Mailbox = arrayUser(0)
Mailbox = "LDAP://" & mailbox
Set objectUser = GetObject (mailbox)
objectUser.Put "altRecipient", arrayUser(1)
objectUser.Put "DeliverandRedirect", True
objectUser.SetInfo
Loop
0
 

Author Comment

by:kt KTAdmin
ID: 33706425
It errors on line 14;  Set objectUser = GetObject (mailbox)

I'll try to work with this.

Thanks!
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33707309
look for one that does not contain the \ character in the DN and only run the script against it
0
 

Author Comment

by:kt KTAdmin
ID: 33708896
How can I modify to use an xlsx file instead of the .txt?
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33708978
vbscript doesn't handle office formats well
it is best to keep in a text file format
0
 

Author Comment

by:kt KTAdmin
ID: 33709933
To confirm your previous statememt, my research shows there is an escape character issue somewhere. Might this help you?

The basic code works fine except in one rare situation. If any member of the
group has a forward slash "/" anywhere in the name you get the error you
describe. ADSI doesn't handle the character correctly. In VBScript the fix
is to escape it with the backslash escape character. For example:
=======
For Each strMemberDN in objGroup.Member
strMemberDN = Replace(strMemberDN, "/", "\/")
Set objMember = GetObject("LDAP://" & strMemberDN)
Wscript.Echo objMember.cn
Next
========
For discussion of characters to escape see this link:

http://www.rlmueller.net/CharactersEscaped.htm

There are several characters that must be escaped if you hard code a
Distinguished Name, but ADSI escapes them all for you (when you use ADSI
methods or attributes) except for the forward slash.



0
 

Author Comment

by:kt KTAdmin
ID: 33709946
Here is the format I use to provison users on our emai system: this is a .csv file: (this works perfectly)

DN      displayName      sAMAccountName      userPrincipalName
CN=Williams\\, Stacey,OU=Admitted1,OU=CAU Students,DC=cau,DC=edu      Williams, Stacey L      stacey.williams      stacey.williams@cau.edu
0
 

Author Comment

by:kt KTAdmin
ID: 33710030
This is what shows in ADSI Edit:

CN=allen\, melanie,OU=Enrolled,OU=CAU Students,DC=cau,DC=edu

However this iswhat shows in the csvde export:
CN=allen\\, melanie,OU=Enrolled,OU=CAU Students,DC=cau,DC=edu

Any idea where the extra "\" is coming from?
0
 
LVL 32

Accepted Solution

by:
endital1097 earned 500 total points
ID: 33712155
no, you should run the script then to update

mailbox = replace(mailbox,"\\,","\,")
0
 

Author Closing Comment

by:kt KTAdmin
ID: 33746653
The user "Genius" was very, very helpful!! He assisted virtually every step of the way. Because our environment is different, I was able to get the script to work. But I'm sure it would in another environment. As is it, we've opted to use a third party solution that will provision our users as needed. But again, I feel got "my monies worth" in my dealings with Mr. Genius. I've learn a lot about vbscripting in the past few days. Thank much!!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Resolve DNS query failed errors for Exchange
Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
In this video we show how to create a Shared Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Sha…
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now