Shortcut Menu

Skip

Main Navigation

Choose your language


ActiveXperts Network Monitor
Monitor servers, workstations, devices and applications in your network

Quicklinks


Windows Scripting Host (WSH) and VBScript

Windows Script Host (WSH) is a Windows administration tool.

WSH creates an environment for hosting scripts. It is language-independent for WSH-compliant scripting engines for the Windows Operating Systems. It brings simple, powerful, and flexible scripting to the Windows platform, allowing you to run scripts from both the Windows desktop and the command prompt.

Windows Script Host is ideal for non-interactive scripting needs, such as logon scripting, administrative scripting, and machine automation. WSH provides improved systems administration capabilities through the use of the WSH Object Model. This runtime library includes objects, methods, and properties that you can use to map network drives or printers, retrieve and modify environment variables, create shortcuts, and read from and write to the Registry. You can use these functions to create logon scripts, configure users' desktops, create custom installation or configuration scripts, or perform other simple tasks.

Although the built-in capabilities of WSH are useful, WSH's real power comes from its ability to use COM to call objects that expose automation interfaces. Microsoft calls this capability ActiveX Scripting. I'll show you an example script that exploits the power of WSH to access the services of Excel and the Active Directory Server Interfaces (ADSI). But first, let's look at how the WSH pieces fit together.

WSH as part of the Operating System

Windows Script Host is built into Microsoft Windows 98, 2000, and Millennium Editions. If you are running Windows 95 or NT4, you can download Windows Script Host 5.6 from the Microsoft Windows Script Technologies Web site at http://msdn.microsoft.com/scripting.

How WSH Works

By default, two script engines are included with WSH: Visual Basic scripting and JavaScript scripting. WSH can be called in two ways:

  1. Windows based (WScript.exe);
  2. Text based (CScript.exe);

The WSH execution environment includes wscript.exe, a windows-based host; cscript.exe, a console-based host; and wshom.ocx, the WSH Object Model runtime library. WSH also includes scripting engines: Microsoft JScript 3.0 (jscript.dll) Microsoft VBScript 3.0 (vbscript.dll). PearlScript, Python (not included by default) And makes use of Object Linking and Embedding (OLE) automation servers, like:

  • ADSI;
  • Excel;
  • Word;
  • Network Component (ActiveXperts Software product);

WSH can handle different source files, and uses the Registry to map source scripts to the appropriate scripting engine. For instance, a source file with the .JS extension will be mapped to the JavaScript Engine.

Wscript.exe and cscript.exe provide the interface between a script source and a registered script engine (i.e., interpreter). The wscript.exe and cscript.exe hosts are language independent. You can write scripts in any language that is registered with WSH. When you double-click a filename with a .js, .vbs, or other registered extension, wscript.exe (the default execution host) executes the script by passing it to one of the registered script engines. WSH selects the appropriate script engine based on the script extension. This information is written to the HKEY_CLASSES_ROOT Registry hive during the script engine registration process. You can change the default execution host to cscript.exe, or you can specify the desired host via the Run dialog box or the command line at the time you invoke your script. For example, from the command line you can enter

cscript c:\myscript.js

or

wscript c:\myscript.vbs

Wscript.exe and cscript.exe also provide several runtime options, including an interactive or batch-mode switch and a maximum number of seconds that a script can run. Both hosts support interactive and batch modes. But cscript.exe's interactive mode uses a command-line interface and wscript.exe's interactive mode uses simple graphical dialog boxes; using either host in batch mode is easy.

To obtain a list of all supported options, at the command line, type

cscript //?

or

wscript //?

WSH Object Model

The WSH Object Model is the runtime library that contains the objects and methods you need to create and control the script execution environment and perform administrative tasks. The WSH scripting engine creates the WScript object at the time you invoke your script; its methods and properties are immediately available. To use the additional WSH methods and properties, you must first create an object of type Shell or Network. For example, you can enter:

Dim objNetwork
Set objNetwork = Wscript.CreateObject( "Wscript.Network" )

to create a Network object. When you obtain a reference to a Network object with the CreateObject Method, you can use the properties and methods these objects expose. In addition to the WSH objects, you'll also use the functions and language constructs of your preferred scripting engine (JScript, VBScript, or other third-party script engines) and any automation interfaces you call, such as ADSI, Excel, Network Component or Internet Explorer.

The scripting engines provide the language support you need to write scripts (e.g., variables, operators, control structures, and functions). The WSH scripting engines are COM based, which provides several important benefits: Third-party language developers can integrate their language into the WSH environment, like Perl and Python. Also, software developers can implement and expose scripting capabilities in applications, and systems administrators can use the scripting language they're most comfortable with (provided a suitable WSH implementation exists).