Computer Performance, VBScript

How to Create a Contact in an Exchange Organization with VBScript

Tutorial for Creating Contacts in an Exchange Organization with a VBScript

On this page, I will explain how to create a Contact which appears in the Exchange GAL (Global Address List).  Example 1 creates a single Contact, where as Example 2 explains how to bulk import the names and addresses from a spreadsheet.  In both examples, there are three crucial fields legacyExchangeDN, mailNickname and targetAddress.

It is only common sense that the more LDAP fields that you have in your script the greater the scope for error.  Therefore, if you are not familiar with creating Contacts, I suggest you start with my simple Contact script.

Topics for Creating Simple Contacts with a VBScript

Our Mission and GoalContact create with active directory users and computers

Always remember that VBScript simply mimics steps that you take manually.  So, try creating a Contact with the Active Directory Users and Computers menus.  However, if the wizard asks for Exchange 2003 information such as Associated Administrative Group, then that is an indication that VBScript will require extra fields, for example, legacyExchangeDN, mailNickname and targetAddress.

There are at least two traps to avoid, firstly, a surprise, we don't need sAMAccountName because the Contact will never actually logon to the domain.

Secondly, rather than the Mail LDAP field, we need to script targetAddress and possibly proxyAddresses.  For a long time I thought VBScript was case insensitive, however with targetAddresses, SMTP (UPPER case) indicates the primary email address, where as smtp (lower case) means a secondary email address.

®

Example 1 - Script to Create a Contact in an Exchange Organization

PrerequisitesContact legacyExchangeDN VBscript to create Contacts

Recommended: that you logon as administrator, preferably at a domain controller.  Remote Desktop would be a suitable alternative.  If that is not possible, you could get these sample scripts to work from an XP machine as a non-administrator, but why introduce extra complications?  At the beginning you want easy success, with fewest obstacles.

Instructions for Creating a Contact in Active Directory

  1. You need access to a Windows Active Directory domain.
  2. Copy and paste the example script below into notepad or a VBScript editor.
  3. Decide whether to change the value for strContainer.
  4. Realistically to configure legacyExchangeDN (strMailbox), you need to create a Contact manually, then examine the properties by exporting with CSVDE or examining with ADSI Edit.  My example is: /o=GuyWorld/ou=First Administrative Group/cn=Recipients/cn=MrAcme, but there is no way in the world that this value will work in your domain.  You must edit at least /o=GuyWorld.
  5. Save the file with a .vbs extension, for example: ExchContact .vbs
  6. Double click contact .vbs and check the strContainer container for strContactName.

Script to Create a Contact in your Active Directory

 

 

' ExchContact.vbs
' Purpose VBScript to create a contact for Exchange 2003
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.4 - August 2005
' --------------------------------------------------------------'

Option Explicit
Dim objRootLDAP, objContainer, objContact, objExcel, objSheet
Dim strOU, strContactName, strPathExcel, strEmail, strProxy
Dim intRow, strYourDescription, strFirst, strLast, strMainDefault
Dim strMailbox, strNick

' Set string variables
' Note: Assume an OU called suppliers exists.
strOU = "OU=Suppliers ," ' Note the comma
strYourDescription = "Guy's Contact"
strMainDefault = "SMTP:acmeMain@acme.com"
strContactName="MrAcme"
strFirst = "Willy"
strLast= "Acme"
strProxy = "smtp:somebody@somewhereElse.com"
strEmail = "not@needed.org"
' ** Please investigate your Exchange Org, then change the next line **
strMailbox = "/o=GuyWorld/ou=First Administrative Group/cn=Recipients/cn="_
& strContactName
strNick = strContactName

' Section to bind to Active Directory
Set objRootLDAP = GetObject("LDAP://rootDSE")
Set objContainer = GetObject("LDAP://" & strOU _
& objRootLDAP.Get("DefaultNamingContext"))

' Build the actual contacts.
Set objContact = objContainer.Create("Contact",_
"cn=" & strContactName)
objContact.Put "Mail", strEmail
objContact.Put "givenName", strFirst
objContact.Put "sn", strLast
objContact.Put "proxyAddresses", strProxy
objContact.Put "targetAddress", strMainDefault
objContact.Put "legacyExchangeDN", strMailbox
objContact.Put "mailNickname", strNick
objContact.SetInfo

WScript.Quit

' End of Sample ExchContact VBScript


VBScript Tutorial - Learning Points

Note 1:  The header section, in the first 11 lines, explains the purpose of the script and declares the variables.

Note 2:  One of the most difficult LDAP fields is legacyExchangeDN.  In the script I manipulate legacyExchangeDN with strMailbox.  The point is that you need to examine your Exchange Organization, then edit /o=GuyWorld.

Note 3:  In many ways this script is messy and convoluted, as you may have guessed, it's preparing you for the bulk import in Example 2, where we read the values from columns in a spreadsheet.

ˇ

Example 2: Sample Script to Create Contacts from Spreadsheet Data

Prerequisites for Creating Contacts from a Spreadsheet

Each Contact will occupy one row of the spreadsheet, for example MrsAcme in Row 3. Each attribute will always be in the same column, for example, everyone's legacyExchangeDN address is in Column J (10th Column).  Here is a sample spreadsheet.

Create a spreadsheet with your prospective Contacts' properties.  My advice is to spend time researching the LDAP attributes, which correspond to what you see in an Active Directory Users and Computers property sheet.  See more on LDAP properties here.

Be aware that where you save this .xls file should correspond to the strPathExcel variable in the script below.  For example: E: \scripts\ContactExcel .xls.

 

 

' BulkContact.vbs
' Example VBScript to Bulk Import Contacts into Exchange 2003
' Author Guy Thomas http://computerperformance.co.uk/
' Version 3.2 - August 2005
' --------------------------------------------------------------'

Option Explicit
Dim objRootLDAP, objContainer, objContact, objExcel, objSheet
Dim strOU, strContactName, strPathExcel, strEmail, strProxy
Dim intRow, strYourDescription, strFirst, strLast, strMainDefault
Dim strMailbox, strNick

' Set string variables
' Note: Assume an OU called suppliers exists.
strOU = "OU=Suppliers ," ' Note the comma
strYourDescription = "Guy's Contact"
strPathExcel = "\\william\ScriptsEzine\VBS\Contact\contactsEx3.xls"
strYourDescription = "Guy's Contact"
intRow = 3 ' Row 1 contains headings

' Section to bind to Active Directory
Set objRootLDAP = GetObject("LDAP://rootDSE")
Set objContainer = GetObject("LDAP://" & strOU _
& objRootLDAP.Get("DefaultNamingContext"))

' Open the Excel spreadsheet
Set objExcel = CreateObject("Excel.Application")
Set objSheet = objExcel.Workbooks.Open(strPathExcel)

' Here is the loop that cycles through the cells
Do Until (objExcel.Cells(intRow,1).Value) = ""
strContactName = objExcel.Cells(intRow, 1).Value
strEmail = objExcel.cells(intRow, 2).Value
strFirst = objExcel.cells(intRow, 3).Value
strLast = objExcel.cells(intRow, 4).Value
strProxy = objExcel.cells(intRow, 5).Value
strMainDefault = objExcel.cells(intRow, 6).Value
strMailbox = objExcel.cells(intRow, 10).Value
strProxy = objExcel.cells(intRow, 5).Value
strNick =strContactName


' Build the actual contacts.
Set objContact = objContainer.Create("Contact",_
"cn=" & strContactName)
objContact.Put "Mail", strEmail
objContact.Put "givenName", strFirst
objContact.Put "sn", strLast
objContact.Put "proxyAddresses", strProxy
objContact.Put "targetAddress", strMainDefault
objContact.Put "legacyExchangeDN", strMailbox
objContact.Put "mailNickname", strNick
objContact.SetInfo

intRow = intRow + 1
Loop
objExcel.Quit
WScript.Quit

' End of Sample ExchContact VBScript


VBScript Tutorial - Learning Points

Note 1:  Observe how we open and close the Excel spreadsheet by manipulating the objExcel object. (Starting at line 27). Set objExcel = CreateObject("Excel.Application")

Note 2:  The Contact's attributes are read from Excel Spreadsheet using the .cell property.

Note 3:  Trace the data in the spreadsheet to lines 31-40 in the VBScript.  At first, it is confusing the way that Column A hold values for the CN, but the VBScript referenced Column A as intRow, 1.  Once you realize that the 1 (in intRow,1), refers to Column A, and 2 would be Column B, then the picture becomes clearer.  Do go back over this method as it is the cornerstone for so many spreadsheet / VBScript interactions.

Note 4:  The Contact object is build with command: objContainer.Create("Contact",_
"cn=" & strContactName). The next 7 lines, (44-53) add values such as strMainDefault to the Contact.  Finally, .SetInfo is rather like pressing the OK button in Active Directory Users and Computers.

Summary for Creating Contacts

Contacts could be a case study for how to employ VBScripts to create Active Directory objects.  The secret is to build your VBScripts gradually.  Start with scripts which contain the actually values.  Then progress to bulk import scripts, which open a spreadsheet and then read the those values from the .cell properties.

Computer Training Software - Recommended Training VideosGuy Thomas recommends Computer Training Software

Their topics and material are ideal for getting you started with VBScript.  The videos are easy to follow and you can control the pace.  Try their free demo material and then see if you want to buy the full package. See more about VB Script Training CD.


See Also

Create Simple Contact       Create a user from a spreadsheet


Introduction to VBScriptDownload my eBook:  Introduction to VBScript - only  $6.25

25+ scripts to get you started with VBScript.  Topics include Active Directory, Network, WMI, File System Object and the Registry.

In addition to the ebook, you get a PDF and a Word version of Introduction to VBScript.

 

 

 

 .


Google

WebComputerperformance.co.uk

If you need printer ink, log online today to find the best deals on ink cartridges around. We have ink for any size printer, laser toner and home toner cartidges. Save when you buy ink with Click Inks.com!

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.