Microsoft Windows Scripting Host
MSDNLibrary April1999
Microsoft Windows Scripting Host
MSDNLibrary April1999
Abstract
The Microsoft® Windows® Scripting Host is a language-independent scripting host for 32-bit Windows operating system platforms. Windows Scripting Host will be integrated into Windows 98, Internet Information Server 4.0, Windows NT® Workstation operating system version 5.0, and Windows NT Server version 5.0.
The Microsoft Windows Scripting Host is a language-independent scripting host for 32-bit Microsoft Windows operating system platforms. Microsoft provides both Visual Basic® Scripting Edition development system and Java Script scripting engines with the Windows Scripting Host. Microsoft anticipates that other software companies will provide Microsoft ActiveX® scripting engines for other languages such as Perl, TCL, REXX, and Python.
Windows Scripting Host can be run from either the Windows-based host (Wscript.exe), or the command shell-based host (Cscript.exe).
The Benefits of Windows Scripting Host
Windows Scripting Host brings simple, powerful, and flexible scripting to the 32-bit Windows platform. Windows Scripting Options Microsoft currently provides three hosts for running these scripting languages across the Windows platform: Command Syntax
Cscript.exe uses the following syntax: Running Scripts Using the Windows-Based Scripting Host There are three ways to run scripts under Windows using the Windows Scripting Host. You can: What Is a .wsh File? The administrator creates a per-script .wsh that specifies settings that the script will use when executed. Administrators can create several versions of the .wsh file for a variety of users within their organizations.
Administrators can use a .wsh file for a specific subset of users within their organizations. This allows the administrator individual control over specific scripts that may be executed on a daily basis.
Administrators can create individual .wsh files for individual users within their organizations. This allows administrators ultimate control over specific scripts that are used within their organizations.
The administrator can use specific .wsh files for logon scripts when users log on to their systems. This provides the administrator individual control over specific script properties that are executed on client machines when users log on.
How to Create a .wsh File
Windows Scripting Host Architectural Overview
The Windows Scripting Host serves as a controller of ActiveX Scripting engines, just as Microsoft Internet
Explorer does. Because the scripting host is not a full Internet browser, it has a smaller memory footprint than
Microsoft Internet Explorer; therefore, Windows Scripting Host is appropriate for performing simple, quick tasks.
The scripting host reads and passes the specified script file contents to the registered script engine through the
IActiveScriptParse::ParseScriptText method provided by the script engine.
Using COM Objects in Windows Scripting Host
The Microsoft® Windows® Scripting Host (WSH) is a scripting utility you can use to run scripts within the base
operating system. The Windows Scripting Host is supported by all Microsoft® Windows® 32-bit operating
systems. You can use Windows Scripting Host to automate common tasks and to create powerful macros and
logon scripts. Windows Scripting Host comes with VBScript and JScript ActiveX scripting engines.
Other software companies provide ActiveX scripting engines for languages such as PerlScript, PScript,
Python, and others.
Implementing More Powerful Scripts
Previously, the only native scripting language supported by the Windows operating system was the MSDOS® operating system command language (Batch Files). Although the MSDOS command language is fast and small, it has limited features compared to Visual Basic Scripting Edition and the JScript™ development software. Today, ActiveX scripting architecture allows users to take advantage of powerful scripting languages such as Visual Basic Scripting Edition and JScript, and MSDOS command scripts are still supported.
Microsoft Internet Explorer
Internet Information Server (IIS)
Windows Scripting Host
Internet Explorer enables scripts to be executed on client machines from within HTML pages.
Internet Information Server now supports Active Server Pages, which enables scripts to be run on Web servers; in other words, it enables server-side scripting over the Internet or an intranet.
Windows Scripting Host enables scripts to be executed directly on the Windows desktop, or from within the command console, without the need to embed those scripts in an HTML document. Scripts can be run directly from the desktop simply by clicking on a script file, or from the command console. Windows Scripting Host provides a low-memory scripting host that is ideal for noninteractive scripting needs such as logon scripting, administrative scripting, and machine automation.
Running Scripts Using the Command-Based Scripting Host
Cscript scriptname.extension [option...] [arguments...]
Host parameters enable or disable various Windows Scripting Host options. Host parameters are always proceeded by two slashes (//).
The script name is the name for the script file, for example, Chart.vbs.
Script parameters are passed to the script. Script parameters are always proceeded by only one slash (/).
Each parameter is optional; however, you cannot specify script parameters without specifying a script. If you don't specify parameters, Cscript.exe displays the Cscript.exe syntax and the valid host parameters. A Windows Scripting Host Command Example
Several sample scripts are installed when you install the final release of the Windows Scripting Host. The sample scripts are also available.
For example, to run Chart.vbs:
From the Start menu, select Programs then select the MS-DOS command prompt.
Run the following commands:
cscript //logo "Drive:"\"Your Sample Directory Here"\chart.vbscscript //nologo "Drive:"\"Your Sample Directory Here"\chart.vbs
When the Windows Scripting Host ships with the Windows NT 5.0 operating system, you will no longer need to provide the script's extension. You will simply be able to type the script name or double-click the script in the Explorer application.
Double-click files and icons in My Computer, Explorer, and the Find window.
Use the Windows Run command and type the full name of the Script to be executed.
Run Wscript.exe from the Run command specifying the script name and optional parameters and arguments.
When you run scripts using the Windows Scripting Host, you can specify which host application, either Cscript.exe or Wscript.exe, is used. You can set the default host application using Cscript at the command prompt using the //H: Scripthostname command.
For example, if you set the default host application to be Wscript.exe and attempt to run the Chart.vbs example, Wscript.exe is the default application for all files with the .vbs extension. The Windows Scripting Host Properties page provides the following options: Setting Per-Script Properties and the .wsh File
By using the Wscript.exe properties page, you are able to set global scripting options for all scripts that are executed on the local machine. However, you also have the ability to set individual script settings on a per-script basis. These script settings allow you individual control over how specific scripts will execute, creating a .wsh file for that script. In order to set per-script properties and create a .wsh file, simply right-click the script file within the Explorer application and choose Properties. Set specific script properties, such as the amount of seconds the script should execute, and choose OK. You will notice that a .wsh file is created in the same directory as the original script and is created using the same name as the original script.
A .wsh file contains per-script settings for script files for Windows Scripting Host. It is like a PIF file for 16-bit applications.
How to Use a .wsh File
Once you have set per-script properties, a .wsh file will be created in the same directory as the original script. The file uses the original name of the script followed by a .wsh extension. Double-click the .wsh file icon in Explorer or use Cscript.exe or Wscript.exe at the command prompt to execute that script. For example,
C:\> cscript Myscript.wsh
How the .wsh File Helps Administrators
Right-click the script file (.vbs or .js) you would like to change properties for. Select the Properties option on the context menu. Change settings within the properties page and select OK or Apply. You will then see a .wsh file created using the same name as the original script that you right-clicked.
What Data Is Stored Within the .wsh File?
The .wsh file is a simple text file and its format is similar to that of an .inf file. Below is an example of a .wsh file:
[ScriptFile]
Path=C:\WINNT\Samples\WSH\showprop.vbs
[Options]
Timeout=0
DisplayLogo=1
BatchMode=0
The Path setting in the [ScriptFile] section points to the script file that this .wsh file affects. The settings found under the [Options] section correspond to settings in the [Script] tab within the Properties dialog box.
How the .wsh File Works
When you double-click the .wsh file or execute the .wsh from the command line, Cscript.exe or Wscript.exe reads the .wsh file to determine the specific script settings that should be used to execute the specific script file. Cscript/Wscript will execute the original script passing in the properties that are defined within the .wsh file. It is important to note that you must have the original script file present when executing the .wsh file. If the .wsh file fails to execute the script, check the Path= entry in the .wsh file to ensure it is pointing to the script you are attempting to run.
The scripting engine does not use the SCRIPT tag or LANGUAGE attribute (used in HTML); instead, it relies on
the extension of the file. This way, the scriptwriter does not have to be familiar with the exact ProgID of various
script engines. The scripting host maintains a mapping of the script extensions to ProgIDs and uses the
Windows association model to launch the appropriate engine.
Note For more information on file extension registration, please see the
Windows Scripting Host Object Model
To use a COM object in a script run by Windows Scripting Host, you must first create an instance of the object.
Once a COM object has been created, you can then use it in scripts.
Windows Scripting Host consists of two applications. One runs scripts from the Windows desktop (WScript.exe);
the other runs scripts from the command prompt (CScript.exe).
To run a script from the desktop, simply double-click on a script file. Script files are text files. By convention,
VBScript files have the extension .vbs and JScript files .js.
To run a script from the command prompt, run the Cscript.exe application with a command line such as the
following
cscript c:\"sample scripts"\chart.vbs
where c:\"sample scripts"\chart.vbs is the path to the file containing the script.
You can print out a list of the parameters supported by Cscript.exe by entering the following command line:
call cscript //?
To use a COM object in a script run by Windows Scripting Host, you must first create an instance of the object. You can do this by calling the CreateObject method (in VBScript) or the ActiveXObject object (in JScript). The following example illustrates calling CreateObject using VBScript:
Dim objXL
Set objXL = CreateObject("Excel.Application")
The following example illustrates creating an ActiveXObject object using JScript:
var objXL = new ActiveXObject("Excel.Application");
Once you have created an instance of the COM object, you can write script that uses the object, for example:
objXL.Visible = true;
In addition to the CreateObject method and ActiveXObject object, both VBScript and JScript provide the method GetObject, which returns an object instance. For more information about GetObject, see the reference documentation for your scripting language.