Script User Account and Mailbox Creation
Create AD user accounts and mailboxes automatically
July 30, 2002
If you administer a network running Windows 2000 Server and Exchange 2000 Server, you or someone you know must create user accounts and mailboxes. Using the Microsoft Management Console (MMC) Active Directory Users and Computers snap-in to perform these tasks manually is tedious, and, like most manual-entry tasks, fraught with opportunities to make data-entry errors. If you currently create users and mailboxes manually, don't despair. In this article, I show you how to use Active Directory Service Interfaces (ADSI), Collaboration Data Objects for Exchange Management (CDOEXM), VBScript, and Windows Script Host (WSH) 5.6 to write a script that automates this process. I review what's required to use the script, describe how to run it, examine what the XML does, and discuss the script details.
The System Requirements
Before you use the script to automatically create user accounts and mailboxes, your network must run Active Directory (AD) and Exchange 2000. Exchange 2000 relies on the AD schema to define mail-enabled AD objects and mailbox-enabled AD user objects. Before or during the Exchange 2000 installation, you must extend the AD schema to support Exchange-specific classes and to update existing classes with Exchange-specific attributes. (Exchange 2000 documentation outlines how to extend the AD schema.)
The client computer on which you intend to run this script must have WSH 5.6 installed. You need WSH 5.6 because the script uses a new and improved method of managing command-line arguments and an XML file format, which earlier versions of WSH don't support. Windows XP comes with WSH 5.6. You can download the WSH 5.6 upgrade for Win2K, Windows NT, Windows Me, and Windows 98 at http://msdn.microsoft.com/scripting. The cdoexm.dll file, which contains the CDOEXM objects, isn't part of the Windows OS. This DLL is installed automatically on computers that run Exchange 2000, and it's also installed automatically with the Exchange Management Components on a client computer. (If you install the Exchange Management UI, you've also installed the Management Components.)
The Script Basics
The script that creates a user account and mailbox is named Create User and Mailbox (CrUM.wsf), which you can download from the Code Library at http://www.exchangeadmin.com, InstantDoc ID 25843. The .wsf extension designates the file type as a Windows Script (WS) file. The XML structure of the .wsf file content provides additional capabilities not available in pure VBScript or JScript scripts. WS files not only provide XML structure but also let you run multiple scripting languages within a single file and reference type libraries. As I detail the parts of CrUM.wsf, I describe the .wsf's file's XML elements. For details about all the XML elements in a .wsf file, see the scripting section of the Microsoft Developer Network (MSDN) Web site at http://msdn.microsoft.com/scripting.
You can run CrUM.wsf from either the WSH graphical interface, wscript.exe, or from the command-line interface, cscript.exe. For command-line help with CrUM.wsf, type
crum.wsf /?
or
crum.wsf
When you run the .wsf file from WScript, CrUM.wsf displays Help and status information in a message box, as Figure 1 shows. When you run the .wsf file from CScript, CrUM.wsf displays the same information in a command window, as Figure 2 shows.
Whether the Help information appears in a message box or in a command window depends on which script host you've configured as the default. WScript is the default script host unless you specifically configure WSH to use CScript. To configure CScript as your default script host, at a command line, type
cscript //h:cscript
and press Enter.
As Figure 1 and Figure 2 show, the .wsf file requires three parameters to create a user account and a mailbox. The /S parameter specifies the Exchange 2000 system on which you want to create the mailbox for the user account. The /U parameter specifies the name of the user account. The /OU parameter specifies the organizational unit (OU) in which you want to create the user account. For example, to create a mailbox on an email server named Exch01 for a user account named EthanW in an OU named TechWriters, type
crum.wsf /s:exch01 /u:EthanW /ou:TechWriters
You must specify the three mandatory parameters. If you omit a mandatory parameter, the .wsf file displays the information that appears in Figure 1 and Figure 2. You can assign many more attributes to a user account object when you create it, but to do so, you must specify additional command-line parameters. For simplicity, I didn't include optional command-line parameters in CrUM.wsf.
The order of the mandatory parameters doesn't matter. For example, if you're creating several users in the TechWriters OU who'll have mailboxes on the Exch01 server, you can reorganize the previous code example to
crum.wsf /s:exch01 /ou:TechWriters /u:EthanW
To create a mailbox for another user on the same server and in the same OU, you simply change the user account name. If any of the parameters contains spaces, you must enclose the value in quotation marks. For example, /U: "Ethan Wilansky" creates a user account named Ethan Wilansky.
The XML Advantage
One reason that the .wsf file uses XML format is to take advantage of the element. In this element, you can document usage information that the .wsf file displays when it runs. Callout A in Listing 1 shows CrUM.wsf's usage information. Later, the .wsf file uses the ShowUsage method to display the contents of the element. Figure 1 and Figure 2 show the displayed information. ShowUsage is a special method that WSH 5.6 includes. If you use the element and the ShowUsage method, you don't have to write special code to provide Help information.
The element, which is the parent of the element, encapsulates one task in a .wsf file. You could define several jobs in this file, but for simplicity, I defined creating a user account and a mailbox as one task and, therefore, as one job. The opening tag appears directly above callout A in Listing 1, and the closing tag appears near the end of Listing 1.
The element is the parent of the element and contains one or more jobs in the .wsf file. Because this .wsf file involves only one job, the element is optional. However, I recommend including it so that you'll be in the habit when you create a .wsf file with multiple jobs..
The
About the Author
You May Also Like