Is your server running slowly? Check with SolarWinds ipMonitor
Get a free evaluation copy of ipMonitor
Contents for Ezine 81 - Contacts - Part 1
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.
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
Scenario: You want to create an Active Directory ContactContacts 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'.
PrerequisitesThis 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
- Decide upon the OU, this is vital. My script uses OU=Suppliers, (note comma). If all else fails substitute cn=users, for OU=Suppliers.
- Copy and paste the example script below into notepad or
use a VBScript editor.
-
One advantage of a good script editor such as OnScript is that you can see the line numbers.
- Save the file with a .vbs extension, for example: Contact.vbs
- Double click Contact.vbs and check your
Active Directory Users and Computers.
- 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.
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.
For a top Script Editor try a free download at OnScript
' Contact.vbs ' Sample VBScript to create a simple contact in Active Directory ' Author Guy Thomas http://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 = "freddy.freebie@freebie.biz"
' 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.
ChallengesTry adding other LDAP properties. You could research with ADSI Edit, for example: Declare: strYourDescription = "Guy's Contact" Script: objContact.Put "description",
strYourDescription
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 john@freebie.biz 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 http://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 PointsNote 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)
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.
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.
|