Guy’s Scripting Ezine 82- Contacts – Part 2

Contents for Ezine 82 – Contacts – Part 2

 ♣

This Week’s Secret

Whereas last week’s contact script will work on any Active Directory domain, this week, it’s essential to have Exchange 200x.  Scripting any object, which contains Exchange properties, is going to be difficult.  The secret is to create the contact object with Active Directory Users and Computers, then dissect, analyze and possibly reverse engineer its LDAP properties.

Scenario: You want to create a Contact for
Exchange’s Global Address List (GAL)

Let us suppose that your users irregularly need to email suppliers, or similar people who are not in your organization.  We will also assume that your users are unable to add these email addresses to their personal address book.  The solutions is a VBscript which adds the contact to the GAL.  Pretend, that as luck would have it, an assistant has created a spreadsheet with all the contacts names and email addresses.  Even with such a clear goal, this is going to be a difficult job, indeed without step-by-step planning, I would go so far as to say that scripting an Exchange contact will be an impossible job.

Exchange Monitor from SolarWindsGuy 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.

Free Download of SolarWinds Exchange Monitor

As I hinted earlier, the best way to learn how toContact create with active directory users and computers bulk import, is to begin by creating a contact manually.  Actually, I mean manually in two senses. Manually as a scripting strategy, create one contact, then progress to a bulk import from a spreadsheet.  Manually as a tactic; launch Active Directory Users and Computers, create a contact.  Be sure to pause at each menu and note down what the wizard asks, remember that your script will need to mimic each step of creating the contact.  I will let you into a secret, when I cannot remember the LDAP property, I launch ADSI Edit (from the support tools).  What I do is seek out the value that I added manually, then read off the corresponding LDAP property.  Typical Microsoft, they provide 3 places to set an email address, thanks to ADSI Edit I discovered that we need an LDAP field called targetAddress (and not Mail). 

Perhaps I should have stressed earlier that legacyExchangeDN is the other key field.  The only way for you to find the correct values is copy the values from a test contact.  For example, if you study my value: /o=GuyWorld/ou=First Administrative Group/_cn=Recipients/cn=.  You realize that you must edit /o=GuyWorld to the name of your Exchange Organization.  Finally, look out for a field called mailNickname.

PrerequisitesContact legacyExchangeDN VBscript to create Contacts

This script needs an Active Directory domain, in addition, you do need an Exchange Organization for the script to work.  Best would be to logon as administrator to a domain controller.  My plan B would be to Remote Desktop to a domain controller.

Instructions for Creating a Simple Contact in Active Directory

  1. Decide upon the OU, this is vital.  (I choose Ou=Suppliers, note the comma.)
  2. Copy and paste the example script below into notepad or use a VBScript editor.
  3. Save the file with a .vbs extension, for example: ContactEx.vbs 
  4. Double click ContactEx.vbs and check your Active Directory Users and Computers.
  5. 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.
  6. One advantage of a good script editor such as OnScript is that you can see the line numbers. 

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.

Example 1 – To Create an Exchange Contact in Active Directory

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

 

‘ ContactEx.vbs
‘ Purpose VBScript to create a contact for Exchange 2003
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 1.6 – 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:[email protected]"
strContactName="MrAcme"
strFirst = "Willy"
strLast= "Acme"
strProxy = "smtp:[email protected]"
strEmail = "[email protected]"
‘ * 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

 

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 where we read the values from columns in a spreadsheet.

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.

Example 2 – To Bulk Import Contacts from a Spreadsheet.

Here is a more realistic scripting example, because we read the names and email addresses from a spreadsheet.  I think of this as the three ‘EXs’, Example, Exchange and Excel combining to create your contacts.  Here is a spreadsheet to give you ideas on how to create your bulk import list.

Prerequisites

  1. You have Exchange 200x installed in your Active Directory Domain.
  2. 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.
  3. You have created an OU=Suppliers, or alternatively, adjusted line 15.
  4. You have an Excel Spreadsheet at e:\ scripts\contactsEx3.xls.  Change the script or create such spreadsheet at this path.
 

‘ BulkContactEx.vbs
‘ Example VBScript to Bulk Import Contacts into Exchange 2003
‘ Author Guy Thomas https://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 = "e:\scripts\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:   Work through the script checking how we open and close the Excel spreadsheet by manipulating the objExcel object. (Starting at line 27). Set objExcel = CreateObject("Excel.Application")

Note 2:  VBScript reads the Contact’s properties from the 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 with Exchange attributes.  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

• 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