Guy’s Scripting Ezine 81 – Contacts – Part 1

Contents for Ezine 81 – Contacts – Part 1

 ♣

This Week’s Secret

I have found the perfect vehicle for practicing scripting – the Contact.  At their simplest, these Contact objects just need a name and an email address.  This week we are going to create the plain Active Directory version of the Contact.  Next week we will investigate the more complex Exchange 2003 version of Contact with its extra LDAP fields, for example LegacyExchangeDN.

To digress, last week from here in England, I watched the Goodwood Races on T.V.  In one race the horse won because its trainer walked the course and found a strip of faster ground and instructed his jockey where to race.  The connection with scripting is this, whenever you create a script, try just walking through the steps manually with GUI.  In this instance, try creating a Contact manually with Active Directory Users and Computers.  This technique will give you a winning edge when it comes to creating your scripts.  You could investigate the properties of this new Contact with ADSI Edit and so discover its crucial LDAP fields.

Scenario: You want to create an Active Directory Contact

Contacts offer an ideal opportunity for building a script gradually.  Firstly, we create just one contact, focussing on the key LDAP properties.  Then in the second example we will bulk import Contacts by reading their names and email addresses from an Excel Spreadsheet.  The focus of the second script is opening the Excel file and reading the cell values.

Incidentally, to me, the enduring magic of scripting is the way that you can loop and repeat instructions.  I truly believe it’s wonderful how fast and precisely a script cycles through code thanks to a ‘For.. Next Loop’; or in the case of Example 2, a ‘Do Until you find a blank cell….. Loop’.

Guy Recommends 3 Free Active Directory ToolsDownload Solarwinds Active Directory Administration Tool

SolarWinds have produced three Active Directory add-ons.  These free utilities have been approved by Microsoft, and will help to manage your domain by:

  1. Seeking and zapping unwanted user accounts.
  2. Finding inactive computers.
  3. Bulk-importing new users.  Give this AD utility a try, it’s free!

Download your FREE Active Directory administration tools.

Prerequisites

This script needs an Active Directory domain, however you do not need an Exchange Organization this week.  I have to admit that you also need Excel to hold the data.  Best would be to logon as administrator to a domain controller.  My plan B would be to Remote Desktop to a domain controller.  Plan C, which does not get the ‘Guy seal of approval’, would be to run the script on an XP client.

Instructions for Creating a Simple Contact in Active Directory

  1. Decide upon the OU, this is vital.  My script uses OU=Suppliers, (note comma).  If all else fails substitute cn=users, for OU=Suppliers.
  2. Copy and paste the example script below into notepad or use a VBScript editor.
  3. One advantage of a good script editor such as OnScript is that you can see the line numbers. 
  4. Save the file with a .vbs extension, for example: Contact.vbs 
  5. Double click Contact.vbs and check your Active Directory Users and Computers.
  6. Remember to seek ‘Refresh’.  If you run the script for a second or third time and then cannot find the new Contacts in Active Directory Users and Computers, don’t rely on F5, right-click the OU and select Refresh from the short cut menu.

Example 1 – To Create a Single Contact in Active Directory

This example creates a contact called MrFreebie in an OU called Suppliers.  I do hope that you adjust the code to make sure it works in your domain.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

‘ Contact.vbs
‘ Sample VBScript to create a simple contact in Active Directory
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 1.3 – August 2005
‘ ————————————————————–‘

Option Explicit
Dim objRootLDAP, objContainer, objContact
Dim strOU, strContactName, strEmail
Dim strYourDescription, strFirst, strLast

‘ Set string variables
‘ Note: Assume an OU called suppliers exists.
strOU = "OU=Suppliers ," ‘ Note the comma
strContactName="MrFreebie"
strFirst = "Freddy"
strLast= "Freebie"
strEmail = "[email protected]"

‘ 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.SetInfo

WScript.Quit

‘ End of Sample Contact VBScript

Learning Points

Note 1:  Every Active Object must have a CN to distinguish it from all other objects.  CN is the first field that the script creates, its value is held by strContactName.

Note 2:  Did you notice anything strange about the LDAP properties?  Did you expect to see sAMAccountName?  Just about every other Active Directory object requires sAMAccountName, but as contacts never logon, as a result no security principle is required.

Note 3:  Pay special attention to the LDAP field called Mail.  This week Mail works perfectly, but for the Exchange variety of Contact you need different properties called targetAddress and proxyAddresses.

Challenges

Try adding other LDAP properties.  You could research with ADSI Edit, for example:
Declare: strYourDescription = "Guy’s Contact"

Script: objContact.Put "description", strYourDescription

Example 2 – To Bulk Import Contacts from a Spreadsheet.

The main challenge of this script is to ‘wire up’ to the Excel Spreadsheet.  The script handles the connection with strPathExcel = "E:\ Scripts\contactsFree.xls".  I would be amazed if you had a file called in contactsFree.xls and I would be even more amazed if it happened to be in a folder called e:\ scripts.  So, the answer if for you to amend the strPathExcel to where ever you hold the spreadsheet.

What is in the spreadsheet you may ask?  One answer is to check the online version of this ezine and then download my example spreadsheet.  Plan b would be to quickly add a few values to a new spreadsheet that you create, for example:  (apologies if the cells don’t line up.)

        Column A Column B       Column C     Column D

Row 1  CN      Mail                givenName  sn

Row 2

Row 3 John    [email protected]  John   Freebie

Eccentrically, I keep Row 2 clear of user data.  I reserve Row 2 to number the columns Column a = 1, b = 2, c=3.   Trust me, this referencing useful when it comes to the Exchange version of the spreadsheet.  In the script I set intRow = 3, which means do not read Contacts until you reach row 3.

 

‘ ContactExcel.vbs
‘ Purpose VBScript to create a contact for Exchange 2003
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 2.5 – August 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
strYourDescription = "Guy’s Contact"
strPathExcel = "E:\Scripts\contactsFree.xls"
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 Contact Spreadsheet VBScript

Learning Points

Note 1:  The File System Object part of the script is as follows, line 26,
‘ Open the Excel spreadsheet
Set objExcel = CreateObject("Excel.Application")
Set objSheet = objExcel.Workbooks.Open(strPathExcel)

Note 2:  The important instruction to cycle through the spreadsheet cells is here on line 30:
Do Until (objExcel.Cells(intRow,1).Value) = "".  See the Loop command

Guy Challenges

My challenges are purely Excel.  Employ formulae to calculate the Contact’s CN and Mail addresses.  For example:

Column A: CN = First name & Last Name.  =C3&D3

Column B: email = First name & ‘@domain.com.  (Note the ‘ mark)

Summary of Creating Plain Contacts in Active Directory.

The Active Directory object called Contact is a classic vehicle to practice VBScript.  Moreover, this ezine emphasises the importance of building a script gradually.  Create one contact, then when that’s working, master Excel, and the File System Object and thus bulk import users stored in the spreadsheets columns.

See More Active Directory VBScripts featuring contacts

• User Spreadsheet  • LDAP Properties  • Create Users  • Bulk Import User Ad  • Ezines

Ezine 23 Enable Accounts  •VBScript Exchange Contact  •VBScript Create Contact  • Tool Kit

Ezine 42 LDAP  • Ezine 44 CSVDE  •Ezine 53 Contacts  • Ezine 81 Contacts  • Ezine 82 Contacts