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.
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.
As I hinted earlier, the best way to learn how to 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.
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
Decide upon the OU, this is vital. (I choose Ou=Suppliers, note the comma.)
Copy and paste the example script below into notepad or use a VBScript editor.
Save the file with a .vbs extension, for example: ContactEx.vbs
Double click ContactEx.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.
One advantage of a good script editor such as OnScript is that you can see the line numbers.
Guy Recommends 3 Free Active Directory Tools
SolarWinds have produced three Active Directory add-ons. These free utilities have been approved by Microsoft, and will help to manage your domain by:
Seeking and zapping unwanted user accounts.
Finding inactive computers.
Bulk-importing new users. Give this AD utility a try, it’s free!
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 UMRA
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.
You have Exchange 200x installed in your Active Directory Domain.
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.
You have created an OU=Suppliers, or alternatively, adjusted line 15.
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
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.
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