Creating Contact
objects with a VBscript can be hard or it can be easy. The difference lies in whether you want to create the object solely in Active Directory, or whether you want to build Contacts for use in an
Exchange Organization.
In order to get a feel for the scripting tasks, create a Contact manually through Active Directory Users and Computers. If Active Directory only asks for two pieces of
information, name and email address, then the script will be straightforward. However, if the wizard asks for Exchange 2003 information such as Information Store, then that is your cue that scripting will
require extra fields, for example, legacyExchangeDN, mailNickname and targetAddress.
On this page, I will show you two examples of scripts that build a Contact with just a name and an email address.
The first VBScript is straightforward. The real challenge comes in the second script where we want to bulk import Contacts by extracting their names and addresses from an Excel Spreadsheet.
Our goal is to create Contacts. Compared with creating Users, the first stage will be easy, because all we need is the name and
email address. Unlike most other Active Directory objects, contacts do not require the sAMAccountName attribute.
The second stage introduces a more realistic scenario, we have a list of
suppliers along with their email addresses in an Excel spreadsheet. Our script will open the spreadsheet and read the column containing the names. This method is often called bulk-import.
Recommended: 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 what value to use for strContainer. Where is your spreadsheet?
Change the values of the variable strEmail to a Contact that you know.
Save the file with a .vbs extension, for example: contact .vbs
Double click contact .vbs. Open Active Directory Users and Computers and check the strContainer container for strContactName. If necessary, 'Refresh' the contents of
your OU.
Script to Create a Contact in your Active Directory
' Contact .vbs ' Purpose VBScript to create a contact object in Active Directory ' Author Guy Thomas http://computerperformance.co.uk/ ' Version
1.3 - May 2005 ' --------------------------------------------------------------' Option Explicit Dim objRoot, objOU, objDomain, objContact, strYourDescription Dim strDNS, strContainer,
strContactName, strEmail
'
Section to attach to Active Directory Set objRoot = GetObject("LDAP://rootDSE") strDNS = objRoot.Get("defaultNamingContext") Set objDomain = GetObject("LDAP://" & strDNS)
' Section to create the contact Set objOU = GetObject("LDAP://"& strContainer & "," & strDNS) Set objContact = objOU.Create("contact", strContactName) objContact.Put "Description", strYourDescription objContact.Put
"Mail", strEmail objContact.SetInfo
Wscript.Echo "Look in " & strContainer & " for (F5) " & strEmail
Note 1: The header section, in the first 10 lines, explains the purpose of the script and declares the variables.
Note 2: The simple, but clever command,
which allows the script to work with any domain
is: GetObject("LDAP://rootDSE"). Crucial, this statement binds WSH / VBScript to Active directory. The next line puts the focus on the OU=Suppliers container, as that is where the
Contact will be
born.
Note 3: .Create is a method which builds an object. In this instance, see how we use "Contact" not "User" or "OU".
Note 4:
Every object, even Contacts, must have a cn (common name). In this example, we use strContactName. The other key attribute is the Mail property, trace how the script supplies the information with strEmail.
We are now ready to bulk import Contacts from a spreadsheet. Just for testing, I create only 3 or 4 sample contacts because I often want to delete them, modify the script and try another run.
Prerequisites for Creating Contacts from a Spreadsheet
Each Contact will occupy one row of the spreadsheet, for example Joshua Kidby in Row 3. Each attribute will always be in the same column, for example, everyone's Mail address is in Column B.
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.
You can download my Spreadsheet here
Be aware that where you save this .xls file should correspond to the strPathExcel variable in the script below. For
example, Line 16 : E: \scripts\ContactExcel .xls.
' ContactExcel .vbs ' Purpose VBScript to create contacts from a list on names in Excel ' Usage where you need contacts for example, suppliers '
Author Guy Thomas http://computerperformance.co.uk/ ' Version 2.2 - May 2005 ' --------------------------------------------------------------'
Option Explicit Dim objRootLDAP, objContainer,
objContact, objExcel, objSheet Dim strOU, strContactName, strPathExcel, strEmail Dim intRow, strYourDescription, strFirst, strLast
' Set string variables ' Note: Assume an OU called suppliers
exists. strOU = "OU=Suppliers ," ' Note the comma strPathExcel = "E:\Scripts\contacts.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 ' Build the actual contacts. Set objContact = objContainer.Create("Contact",_ "cn=" & strContactName)
objContact.Put "Mail", strEmail objContact.Put "givenName", strFirst objContact.Put "sn", strLast objContact.SetInfo intRow = intRow + 1 Loop
objExcel.Quit
WScript.Quit
' End of Sample ContactExcel VBScript
VBScript Tutorial - Learning Points
Note 0: Brian K suggests the following amendment to Line 36
You might want to change the line in the ContactExcel.vbs script as below to handle a "," in the CN
From:
Set objContact = objContainer.Create("Contact","cn="
_
& strContactName)
To:
Set objContact = objContainer.Create("Contact","cn=" _
& Replace(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") Set
objSheet = objExcel.Workbooks.Open(strPathExcel)
Note 2: The Contact's name and email address are stored in the .cell property of the
Excel Workbook.
Note 3: Trace the data in the spreadsheet to lines 31-35 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 intRow,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 3 lines, (39-41) add values such as strEmail
to the Contact. Finally, .SetInfo is rather like pressing the OK button in
Active Directory Users and Computers.
The best way to create Contacts is from a list of names in a spreadsheet. From a learning point of view, it is best to investigate how to create a single Contact, then progress to the Excel Spreadsheet method. As
with other VBScripts, this scripts mimics what you would do manually if you needed to create a Contact in Active Directory Users and Computers.
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.