Scripting a New Mailbox
After you've selected the attributes to use in the new mailboxes, you write a short batch file to incorporate command-line parameters and create a new mailbox. For example, I created newmb.bat, which Listing 1 shows. I started each section of the script with a REM comment. The first section of the script echoes the contents of the header information to a file with the user's name. When you write your script, the header must contain the attributes you selected. The second section contains the information for each attribute in the header.
To demonstrate how this batch file works, let's create a mailbox for new user Jennifer Hansen. Jennifer already has a domain account, jenniferh, in DomainA. Jennifer's domain account will be the primary account for the mailbox.
The variable %1 represents the username in the domain account, %2 represents the user's first name, and %3 represents the user's last name. The script uses the variables to customize the import file. You provide the values for these variables when you launch the script from the command-shell window. In this example, you would type
newmb.bat jenniferh Jennifer Hansen
on the command line to launch the script and provide the three variable values. (For more information about using environmental variables to temporarily hold a value, see Dick Lewis, "Shell Scripting 101, Lesson 3," http://www.winscriptingsolutions.com, InstantDoc ID 20142.)
The first two sections in the script use the %1, %2, and %3 variables to create a customized, text-based import file from the generic CSV-based import file you created earlier. The script names the customized import file based on the username (in this case, jenniferh.txt) and saves the file to \\adminhost\scripts. (You can customize the location by changing this path in the script.) In the customized import file, attributes containing no information display with two consecutive commas in the data field. The first two sections create the import file and save it to \\adminhost\scripts. The import file is named jenniferh.txt based on the username variable.
After the first two sections of the script create the customized import file, the last section in the script uses that file to create an Exchange mailbox. As callout A in Listing 1 shows, the script uses the Exchange admin utility to create the mailbox.
The /i switch sets admin.exe to import mode so that admin.exe can import the new mailbox. If you changed the location in which you saved this file, you need to replace \\adminhost\scripts with your directory path. The /d switch designates the server name that contains the directory to update. Thus, you might need to replace exchsrvr with your server's name. For reference, the /? switch describes all the command-line parameters.
When you run admin.exe with /i, Exchange makes several assumptions about how to perform the import. For example, the program makes imports to the Recipients container by default. You can modify defaults by creating an options file and using the /o switch. Figure 2, shows the makeup of an options file and what the import defaults are. In most cases, you don't need to create the options file.
After newmb.bat executes, you'll have a new mailbox with six fields: first name, last name, alias, display name, primary NT account (assuming the username jenniferh exists), and multiple email addresses. For more information about how to write scripts that create new user accounts, see "Take Command of Your Management Tasks," http://www.win2000mag.com, InstantDoc ID 16426. The Microsoft article "XADM: Description of Directory Export/Import Event IDs" (http://support.microsoft.com/support/kb/articles/q184/5/32.asp) provides a list of error codes related to imports.
Better Late than Never
Creating mailboxes from the command line is nothing new to Exchange 5.5. However, many administrators continue to rely on the GUI for mailbox creation. Scripting mailboxes promotes consistency, reduces errors, and maintains standards. Possibly the best benefit of scripting is the time you save compared with manually creating mailboxes with the GUI. Try importing mailboxes in your test lab to get a feel for how easy the process is. You'll wonder why you ever created mailboxes any other way.