Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1836
  • Last Modified:

Sample vbscript to set Forwarding to external contacts.

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
Anthony K O365
Asked:
Anthony K O365
  • 33
  • 27
1 Solution
 
endital1097Commented:
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
 
endital1097Commented:
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
 
Anthony K O365Author Commented:
Thanks. I will give it a try!!
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
Anthony K O365Author Commented:
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
 
endital1097Commented:
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
 
Anthony K O365Author Commented:
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
 
endital1097Commented:
text file containing all the DNs
0
 
Anthony K O365Author Commented:
OK
0
 
Anthony K O365Author Commented:
So I need to include the altRecipient value since that will be included in the script. Correct?
0
 
endital1097Commented:
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
 
Anthony K O365Author Commented:
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
 
endital1097Commented:
you need to have the user dn and the forwarding dn
then use the script
0
 
Anthony K O365Author Commented:
Please explain. Are these two separate files? Also, I'm using Exchange 2003 sp2.
0
 
endital1097Commented:
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
 
Anthony K O365Author Commented:
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
 
endital1097Commented:
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
 
Anthony K O365Author Commented:
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
 
endital1097Commented:
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
 
Anthony K O365Author Commented:
It errors on line 11 "Server not operational"

this line 11:   Set objUser = GetObject ("LDAP://" & arrayUser(0))
0
 
endital1097Commented:
put the following in front of that line to verify the value

WScript.Echo arrayUser(0)
0
 
Anthony K O365Author Commented:
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
 
endital1097Commented:
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
 
Anthony K O365Author Commented:
"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
 
Anthony K O365Author Commented:
Also this

strOU = "OU=Enrolled,OU=Cau Students
0
 
endital1097Commented:
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
 
Anthony K O365Author Commented:
I'm sorry. I don't quite understand. What would the script look like afterwards? The text file as well?
0
 
endital1097Commented:
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
 
Anthony K O365Author Commented:
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
 
endital1097Commented:
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
 
Anthony K O365Author Commented:
the Wscript.Echo shows only CN=allen\\, Melanie,OU=Enrolled,OU=CAU Students,DC=cau,DC=edu
0
 
endital1097Commented:
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
 
Anthony K O365Author Commented:
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
 
Anthony K O365Author Commented:
it errors on line 15 which is Set objectUser = GetObject (mailbox)

the Wscript.Echo shows: LDAP://"CN=allen\\, Melanie,OU=Enrolled...
0
 
endital1097Commented:
why are there " in there
are they in the text file
0
 
Anthony K O365Author Commented:
Yes, since there are spaces in the line therefore I include " " Not correct?
0
 
Anthony K O365Author Commented:
I get "invalid DN syntax" w/o the " "
0
 
Anthony K O365Author Commented:
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
 
endital1097Commented:
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
 
Anthony K O365Author Commented:
Again, I truly thank for your help and patience!! I'll continue tomorow.
0
 
endital1097Commented:
i'm going to create an account like yours in my lab and make it work
0
 
endital1097Commented:
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
 
endital1097Commented:
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
 
Anthony K O365Author Commented:
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
 
endital1097Commented:
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
 
Anthony K O365Author Commented:
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
 
endital1097Commented:
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
 
Anthony K O365Author Commented:
It errors line 34:  Mailbox = Replace(Mailbox, "\\," "\\\,")
0
 
endital1097Commented:
sorry missed a comma
Mailbox = Replace(Mailbox, "\\,","\\\,")
0
 
Anthony K O365Author Commented:
errors line 17, 1st character:  Set objectUser = GetObject (mailbox)
0
 
Anthony K O365Author Commented:
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
 
endital1097Commented:
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
 
Anthony K O365Author Commented:
It errors on line 14;  Set objectUser = GetObject (mailbox)

I'll try to work with this.

Thanks!
0
 
endital1097Commented:
look for one that does not contain the \ character in the DN and only run the script against it
0
 
Anthony K O365Author Commented:
How can I modify to use an xlsx file instead of the .txt?
0
 
endital1097Commented:
vbscript doesn't handle office formats well
it is best to keep in a text file format
0
 
Anthony K O365Author Commented:
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
 
Anthony K O365Author Commented:
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
 
Anthony K O365Author Commented:
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
 
endital1097Commented:
no, you should run the script then to update

mailbox = replace(mailbox,"\\,","\,")
0
 
Anthony K O365Author Commented:
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

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.

  • 33
  • 27
Tackle projects and never again get stuck behind a technical roadblock.
Join Now