Making a Single-Task Command-Line Application or Script Perform Repetitive Tasks


Can I run a script that's designed to perform a single task so that it will perform tasks repetitively—without modifying the script? Specifically, I want to pass usernames from a text file to the Crum.wsf utility that you created in "Script User Account and Mailbox Creation," September 2002, InstantDoc ID 25843.

The For command comes in handy in this situation. The command can read each line of a text file—let's use a file named names.txt as an example—and pass the information on each line to the command that the For command runs.

The Crum.wsf utility requires three parameters to create a user account and a mailbox. The /s parameter specifies the Microsoft Exchange Server 2003 or Exchange 2000 Server 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 and a corresponding user account named EthanW in an OU named TechWriters, use the command

crum.wsf /s:exch01 /u:EthanW /ou:TechWriters

To run this command multiple times for different usernames, create a text file that contains each user-account name on a separate line, as Figure 1 shows. Then, to run Crum.wsf multiple times and create mailboxes on Exch01 along with the specified user accounts in an OU named TechWriters, you'd use the command

FOR /f %i IN (names.txt) DO cscript crum.wsf /s:exch01 /u:%i /ou:TechWriters

If you want to create the user accounts in different OUs, you can add the OU names to names.txt, as Figure 2 shows, then further refine the For command as follows:

FOR /f "tokens=1,2 delims=," %i IN (names.txt) DO cscript crum.wsf /s:exch01 /u:%i /ou:%j

The tokens option tells the For command that each line of the text file contains two items. In this case, the first item is the user account name and the second item is the OU in which you want to create the account. Note that there is no space between the account name and the OU; the comma delimits the two values. If you add a space after the comma and before the OU value, Crum.wsf will fail with an object required error. This failure happens because the OU value is padded with a space in the names.txt file. You can solve this problem by trimming the space in the Crum.wsf script file, by avoiding using spaces in the text-file values that the For command reads, or by enclosing the second parameter in the For command in double quotes, as follows:

FOR /f "tokens=1,2 delims=," %i IN (names.txt) DO cscript crum.wsf /s:exch01 /u:%i /ou:"%j"

When the For command runs, the %i variable replaces the value of the /u command-line parameter near the end of the command and the %j variable replaces the value of the /ou command-line parameter. Make sure that you create the specified OUs in Active Directory (AD) before you run the For command.

Notice that you don't specify the %j variable near the beginning of the For command. Because you specify that there are two variables (called tokens in the options section of the For command), the For command assigns %i as the first variable (shown as %i near the beginning of the For command) and %j as the second variable. The first variable (%i) is called an explicit variable because it's specified on the command line; the second variable (%j) is called an implicit variable because it isn't specified on the command line. When the %j variable is enclosed in double quotes, the For command ignores any spaces that might have been added in the names.txt file or in the OU name.

Note also that variables are case sensitive in the For command. Therefore, %I and %i are not considered the same variable. However, the Crum.wsf script doesn't make this distinction with its command-line switches. Therefore, /u and /U are equivalent, as are /ou and /OU.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.