5
Introducing Windows Scripting
1
ensure that Windows Script Host is installed on your system, type
cscript at a command prompt.
You should see version information for Windows Script Host as well as usage details. If you don’t
see this information, Windows Script Host may not be installed and you’ll need to install it as you
would any other Windows component.
The key components of Windows Script Host are as follows:
WScript:
■
A Windows executable for the scripting host that is used when you execute
scripts from the desktop. This executable has GUI controls for displaying output in pop-up
dialog boxes.
CScript:
■
A command-line executable for the scripting host that is used when you
execute scripts from the command line. This executable displays standard output at the
command line.
WSH ActiveX Control:
■
An ActiveX control that provides the
core object model for the
scripting host.
Scripting Engines:
■
Scripting engines provide the core functions, objects, and methods
for a particular scripting language. VBScript and JScript scripting engines are installed by
default on Windows.
A Windows script is a text file containing a series of commands. Unlike shell scripts, Windows
script commands don’t resemble commands that you’d type in at the keyboard. Instead, they follow
the syntax for the scripting language you are using, such as VBScript or JScript.
Windows scripts can be created in Notepad. When you
finish creating the script, save it with an
extension appropriate for the scripting language (.vbs for VBScript, .js for JScript, or .wsf for batch
scripts that combine scripts with markup). Once you create a Windows script, you run it with
WScript or CScript.
Using and running scripts
Windows scripts can be run with either WScript or CScript, and
most of the time the applica-
tion you use depends on your personal preference. However, you’ll find that WScript works best
for scripts that interact with users, especially if the script displays results as standard text output.
For tasks that you want to automate or run behind the scenes, you’ll probably prefer CScript, with
which you can suppress output and prompts for batch processing.
You can use WScript and CScript with scripts in several different ways. The easiest way is to set
WScript as the default application for scripts and then run scripts by clicking their file name in
Windows Explorer. Don’t worry—you don’t have to do anything fancy to set WScript as the default.
The first time you click a Windows script, you’ll be asked if you’d like to
associate the file extension
with WScript. Click Yes. Alternatively, you may see an Open With dialog box that asks which pro-
gram you would like to use to open the file. Choose WScript, and then check the “Always use this
program to open this file” checkbox.
86804c01.indd 5
86804c01.indd 5
1/21/09 1:16:18 PM
1/21/09 1:16:18 PM
6
Part
I
Getting Started with Windows Scripting
You can also set CScript as the default interface. When you do this, clicking a Windows script runs
CScript instead of WScript. Or, you could run scripts from the Run prompt just as you could when
WScript was the default. To run scripts with CScript
from the command line, enter
cscript followed
by the pathname of the script you want to execute. For now, don’t worry about the details; you’ll
find detailed instructions in Chapter 4.
Core object model
The core object model and scripting hosts are packaged with WSH for Windows. The core object
model is implemented in the WSH.ocx ActiveX control. WSH.ocx provides the key functionality
necessary for scripts to interact with the operating system. In WSH, objects are simply named con-
tainers that you’ll use to interact with operating system components. For example, you’ll use the
WshNetwork
object to access and configure network resources, such as printers and drives.
Each object has properties and methods that are used to perform certain types of tasks.
Properties
are attributes of an object that you can access. Methods are procedures that you’ll use to perform
operations. As with other object-based programming languages, you can work with objects in a vari-
ety of ways. You can use built-in objects, create new objects based on the built-in objects, or define
your own objects using unique methods and properties.
Table 1-1 provides a summary of the WSH object model. The WSH object hierarchy can be bro-
ken down into two broad categories: exposed objects and non-exposed objects.
Exposed objects,
such as
WScript
, are the ones you’ll work with in your scripts. Non-exposed objects, such as
WshCollection
, are accessed through the methods or properties of other objects. These objects
do the behind-the-scenes work.
TABLE 1-1
Core WSH Objects
Object Type
Object
Description
Exposed Object
Script.Signer
An object that allows you to sign scripts with a
digital signature and to verify signed scripts
WScript
Top-level object that provides access to core
objects and other
functionality such as object
creation
WScript.WshNetwork
Automation object used to access and configure
network resources, such as printers and drives, also
provides user, domain, and computer information
WScript.WshShell
Automation object that provides access to the
environment and file folders
86804c01.indd 6
86804c01.indd 6
1/21/09 1:16:18 PM
1/21/09 1:16:18 PM