The Code
Now let's look at the example script, AddUsers.vbs. Listing 1 shows the complete script, which adds users to an NT 4.0 directory in batch mode. AddUsers.vbs is an adaptation of a sample script of the same name that WSH includes. AddUsers.vbs demonstrates basic use of the WSH Object Model, basic use of the VBScript 3.0 scripting engine, and WSH's ability to create and control OLE automation servers.
To successfully run the example script, you'll need WSH 1.0, ADSI 2.0, and Excel 97 installed on your workstation. (For information about a DLL bug in WSH, see the online supplement, "The WSH Bug," at http://www.winntmag.com.) To install WSH 1.0, download the wsh.exe self-extracting executable from http://www.microsoft.com/management/wsh.htm. To install ADSI 2.0, download adsx86.exe from http://backoffice.microsoft.com/downtrial/moreinfo/adsi2.asp. Follow the ADSI 2.0 Setup instructions at this URL. Note that ADSI requires NT 4.0 Service Pack 3.
AddUsers.vbs begins by declaring the variables in the script. VBScript has only one data type: variant. You can use variants to store various subtypes including Boolean, byte, integer, currency, floating-point, date/time, string, and object. In VBScript, declarations are optional unless you use the Option Explicit statement; Option Explicit forces you to declare all variables before you use them. Next, the script initializes carriage return/line feed (CRLF) and dsRoot, respectively. The dsRoot variable contains the ADSI and NT 4.0-compliant directory name. Change LAB to your NT 4.0 test domain name to run this script in your local environment.
Next the script creates the wshShell object. You must include this step to use wshShell's Popup method. Popup's optional second argument lets you define how long (in seconds) to display the dialog box before the script continues. To avoid the overhead of creating the wshShell object, use VBScript's MsgBox function. Popup uses the same constants and return values as VBScript's MsgBox function. After creating the wshShell object, the code displays the popup dialog box that tells the user what action the script performs. If the user doesn't click Yes within 60 seconds, the script silently exits.
If the user clicks Yes, the script continues to the code at callout A, which obtains the path and file specification to the Excel file that contains the list of users to create. The script provides two mechanisms for the user to supply this information: via the command line at the time you invoke the script or via a prompt using VBScript's InputBox function. The code at A starts by assigning Wscript.Arguments to argv, which lets you query the properties that the WshArguments object provides. For example, the sample script uses argv.Count to test for the number of arguments. If the number of command-line arguments is zero, the script prompts the user for the file. Otherwise, the script assigns the first command-line argument, argv.Item(0), to xlFile. If no file specification is supplied using either input, the code displays an error message to the user for 10 seconds and kills the script.
Next, the script creates an Excel automation object and assigns to xlObj the reference Wscript.CreateObject("EXCEL.application") returns. When you have a valid reference to an EXCEL.Application object, you can use the objects, methods, and properties available in the Excel object library, as B shows. This code opens the user-defined input file, activating the AddUsers sheet and positioning the cursor in the A2 starting cell. The sample script has a rigid dependency on the format of the Excel file. Screen 1 shows an example of the input file I used to test the script.
Before cranking out users, you must obtain a reference to the NT 4.0 directory via ADSI. For this reference, AddUsers.vbs uses VBScript's GetObject function. The code passes GetObject to the ADSI and NT 4.0-compliant name-space name (WinNT://LAB in this example), and returns a reference to the directory in dsObj. You can use this reference to apply the methods and properties available via ADSI.
At the beginning of the Do While loop, the script tests the contents of the A2 active cell. If the string is not zero-length, the script defines the ADSI user object you want to create (usrObj), sets the object's properties (including FullName, Description, HomeDirectory, LoginScript, and Profile), and writes the newly defined object to the SAM database with SetInfo. At C, the script discards the user object in preparation for the next loop iteration, advances the focus in the Excel file to the next row, and begins again. The script continues until it hits the first empty cell in the first column of the Excel file. On exiting the loop, the script kills Excel, displays a success message for 10 seconds, and quits.
Scripts and the Web
Scripting is an important piece of any successful component architecture. Look no further than the Web for the most successful component architecture and implementation to date. Scripts played an important role in making the Web what it is today, and WSH is an example of Microsoft's component strategy. Is WSH Microsoft's COM glue? The fact that WSH scripts are appearing with products like Microsoft Transaction Server supports this idea. With WSH, NT systems administrators might have a scripting solution that works.
I feel lucky I could so much valuable information on wscript on your site, eventhough I have to admit I discovered them by myself. As I am fighting against my Win NT4.0 server down here in Belgium, attempting to schedule a script, I had to look further on Task Manaer, AT command and wscript modes.
Maybe could you explain me or redirect me to one knowing, why I can't schedule a VBS script. It runs fine at MS_DOS prompt, with or without "wscript" prefix, but each time I place some AT command, it reschedule it for the next day and in the Task Manager (Alt-Ctr-Del) I can see the WSH pending. I tried several wscript options 5//B batch, //NoLogo,...)... no success.
Thanks for your kind assistance,
Best regards
SLAVIC Alexis September 19, 2002