Windows Scripting Host (WSH) is an effective scripting host for performing administrative tasks. However, WSH's GUI offers only VBScript's MsgBox and InputBox functions when you run administrative scripts. These functions have limited capabilities. Some limitations are minor. For example, you can't remove the VBScript prefix from the caption on the title bar or customize the labels of the buttons. However, other limitations are more significant. For example, if you need to use a password to run a script and you don't want to hard-code your plaintext password in the script, you must pass in your plaintext password through the command line or through InputBox. From a security standpoint, neither approach is desirable because you can't mask the password as you enter it.
To work around these limitations, I created an ActiveX control called ImprovedInputBox (ImprovedInputBox.ocx). ImprovedInputBox eliminates several of InputBox's limitations. With ImprovedInputBox, you can set the title-bar caption and the command-button labels. As a result, you can create an input box in German, Italian, or another language. With ImprovedInputBox, you can create an input box that prompts users to set or enter a password and then masks the password as they type it.
Another advantage is that ImprovedInputBox lets you know which buttons users activate. With InputBox, if a user clicks Cancel, the input box returns an empty string. However, you can also get an empty string if a user clicks OK but doesn't fill in a field. InputBox doesn't make a distinction between these two cases, but ImprovedInputBox does. Even better, you can use ImprovedInputBox in JScript code because you implement ImprovedInputBox as a separate ActiveX control.
I used Microsoft Visual Basic (VB) 5.0 Control Creation Edition to create ImprovedInputBox. You can find ImprovedInputBox in the Code Library on the Win32 Scripting Journal Web site at http://www.win32scripting.com. But before I explain how to install and use this ActiveX control, let me show you how it works.
How ImprovedInputBox Works
As Screen 1, page 2, shows, ImprovedInputBox consists of three forms (i.e., windows), one module (a module typically contains general procedures related to a form), and one user control (a user control typically contains event procedures related to a form). The forms provide the graphical elements for three input boxes:
To use an input box's returned values in WSH, you must save those values in public variables before the input box closes. The module mdlImprovedInputBox contains code that declares these variables. The public variables are
After the values are in the public variables, you can use the methods and properties of the ImprovedInputBox object to retrieve the returned values. The user control InputBoxCollection contains code that defines each method and property. The InputBoxCollection object has three methods (ShowInputBox, ShowSetPasswordBox, and ShowEnterPasswordBox) and three properties (GetInput, GetPassword, and GetButtonPressed).
ShowInputBox. As Screen 2 shows, the ShowInputBox method displays an input box that prompts users to enter input. The method's syntax is
If a user clicks the confirm button or presses Enter, ShowInputBox saves the string that the user typed in the text box. If the user clicks the abort button or presses Esc, the method saves a zero-length string.
ShowSetPasswordBox. As you can see in Screen 3, the ShowSetPasswordBox method displays an input box that prompts users to set a password. Users enter the password in two fields: Password and Retype.
ShowSetPasswordBox's syntax is
ShowSetPassword's prompt, confirm, and abort arguments are the same as those for ShowInputBox. The title argument is similar, except that its default is Set Password Box. The password character argument is the character that masks the password the user types into the password box. If you omit the password character argument, the input box uses an asterisk (*) as the default value.
When a user clicks the confirm button or presses Enter, ShowSetPasswordBox saves the passwords that the user entered in the Password and Retype fields. To ensure that the user types the password correctly, the method checks whether the two passwords match. If they don't match, ShowSetPasswordBox displays the password box again. This feature is typical for input boxes in which users set a new password or change an existing password. If the user clicks the abort button or presses Esc, the method returns a zero-length string.
ShowEnterPasswordBox. As you can see in Screen 4, the ShowEnterPasswordBox method displays an input box that prompts users to enter their passwords. The method's syntax is
where the arguments are the same as those for ShowSetPasswordBox, except for the title argument. In ShowEnterPasswordBox, the title argument's default is Enter Password Box.
If a user clicks the confirm button or presses Enter, ShowEnterPasswordBox saves the password that the user entered in the text box. If the user clicks the abort button or presses Esc, the method saves a zero-length string.
GetInput. The GetInput property retrieves the value in the pubstrInput variable. The value is the input that the users entered in the input box you created with the ShowInputBox method. If you use GetInput without ShowInputBox, the property returns an empty string. GetInput also returns an empty string if the user clicks the abort button or presses Esc. (You can use the GetButtonPressed property to determine which is the case.) GetInput has no arguments.
GetPassword. This property retrieves the value in the pubstrPassword variable. The value is the input that the users entered in the input box you created with the ShowSetPasswordBox or ShowEnterPasswordBox method. If you use GetPassword without ShowSetPasswordBox or ShowEnterPasswordBox, the property returns an empty string. GetPassword also returns an empty string if the user clicks the abort button or presses Esc. (Once again, you can use the GetButtonPressed property to determine which is the case.) GetPassword has no arguments.
GetButtonPressed. The GetButtonPressed property retrieves the value from the pubstrButtonPressed variable. That value is the label of the button that the users activated in the input box you created with the ShowInputBox, ShowSetPasswordBox, or ShowEnterPasswordBox method. GetButtonPressed has no arguments.
How to Install ImprovedInputBox
You can use ImprovedInputBox on a Windows 2000 (Win2K), Windows NT 4.0, or Windows 98 system. However, you must have the VB 5.0 runtimes on that system. You can download VB 5.0 Control Creation Edition from Microsoft at http://msdn.microsoft.com/vbasic/downloads/cce.
After you download ImprovedInputBox.ocx, run the following command from the command line or the Run menu on the machine that will use the ActiveX control:
This command registers ImprovedInputBox with your system. Depending on your system, you might need to specify a fully qualified path name for this ActiveX control.
If you don't need ImprovedInputBox any longer, you can unregister it by typing
This command removes all Registry entries.
How to Use ImprovedInputBox
The best way to demonstrate how to use ImprovedInputBox is with the example script in Listing 1. This script implements the three methods to produce four input boxes. Here's how the script works.
After declaring Option Explicit and the variable objInput, the script creates an instance of the InputBoxCollection object and sets that instance to objInput. The script then calls the InputBoxCollection object's ShowSetPasswordBox method and passes the input box's message (in this case, the string MyPrompt) to the mandatory prompt argument. All the other arguments are optional. If you don't include them, the resulting input box displays the default values (see Screen 3).
Next, the script uses the InputBoxCollection object's GetButtonPressed property to find out which button the user pressed and the GetPassword property to retrieve the password that the user entered. The script uses WSH's Echo method to display both values.
The code at callout A in Listing 1 shows how you can customize an input box for setting passwords. The script again calls the ShowSetPasswordBox method. However, instead of using the default values, the script sets all the arguments. Screen 5 shows the resulting customized input box for setting passwords.
The code after callout A calls the InputBoxCollection object's ShowEnterPasswordBox method. The script then sets all the arguments to produce a customized input box for entering passwords.
The remaining code calls the InputBoxCollection object's ShowInputBox method and sets all arguments. To retrieve the value that the user entered in the input box, the script uses the InputBoxCollection object's GetInput property.
Use As Is or Adapt for Further Capabilities
Creating better input boxes isn't difficult and can make your job a lot easier. You just need to download ImprovedInputBox and install it in your system. You can use the defaults to make a standard input box or use the optional arguments to create customized input boxes.
You can extend ImprovedInputBox's capabilities even further by adapting this ActiveX control and its source code. For example, you can add check boxes or drop-down lists. When I created ImprovedInputBox, I used Günter Born's Inside Windows Scripting Host (Microsoft Press Germany, 1999) as a guide. This book has a detailed chapter on how to use VB 5.0 to develop ActiveX controls for use in WSH. This chapter also covers pitfalls to avoid. Inside Windows Scripting Host is only available in German at this time. However, Born is in the process of translating his book into English. (For more information about the translation, go to http://ourworld.compuserve.com/ homepages/guenter_born/index0.htm and click Windows Scripting Host Tutorial.) Until he finishes this translation, you can check out the other resources that the sidebar "Additional Resources" lists.