How to Create a Contact in an Exchange Organization with VBScript
VBScript for Creating Contacts in an Exchange Organization
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
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
Prerequisites
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
You need access to a Windows Active Directory domain.
Copy and paste the example script below into notepad or a VBScript editor.
Decide whether to change the value for strContainer.
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.
Save the file with a .vbs extension, for example: ExchContact .vbs
Double click contact .vbs and check the strContainer container for strContactName.
Guy Recommends: SolarWinds' Free Bulk Mailbox Import Tool
Import users from a spreadsheet, complete with their mailbox. Just provide a list of the
users with the fields in the
top row, and save as .csv file. Then launch this FREE utility, match
your Exchange fields with AD's
attributes, click and import the users. Optionally, you can
provide the name of the OU where the new mailboxes will be born.
There are also two bonus tools in the free download, and all 3 have been approved by Microsoft:
Bulk-import new users and mailboxes into Active Directory.
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 2010 ' -----------------------------------------------'
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 check your Exchange Org,
and change 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"))
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.
Guy Recommends:
The SolarWinds Exchange Monitor
Here is a
free tool to monitor your Exchange Server. Download and
install the utility, then inspect your mail queues, monitor the Exchange
server's memory, confirm there is enough disk space and check the CPU
utilization.
This is the real deal - there is no catch. SolarWinds provides this
fully-functioning freebie, as part of their commitment to supporting
the network management community.
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 2010 ' -----------------------------------------------'
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
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.
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.
See More Active Directory VBScripts featuring contacts
Windows Management Instrumentation (WMI) is
most useful for PowerShell scripting.
SolarWinds
have produced this
Free WMI Monitor to take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.