7
Introducing Windows Scripting
1
Object Type
Object
Description
WshController
Automation object that provides the control
functions necessary for creating a remote script
process
Non-exposed Object
WshArguments
Accessed through the WScript.Arguments
property, obtains command-line arguments
WshCollection
Accessed through WshNetwork.Enum
Network
Drives
or WshNetwork.EnumPrinter
Collection
, used for iteration through a group
of items, such as printers or drives
WshEnvironment
Accessed through the WshShell.Environment
property, allows you to work with environment
variables
WshNamed
Accessed through the WScript.Arguments
.Named
property, allows you to work with named
arguments passed to a script
WshRemote
Accessed through the WshController
.WshRemote
method, allows you to start, stop,
and track the status of remote scripts
WshRemote.Error
Accessed through the WshRemote.Error
property, used to track runtime errors related to
remote scripts
WshScriptExec
Accessed through the WshShell.Exec method,
allows you to track the status of program or scripts
started with the WshShell.Exec method, also
provides access to the related input, output, and
error streams
WshShortcut
Accessed through the WshShell.CreateShortcut
method, used to create and manage file shortcuts
WshSpecialFolders
Accessed through the WshShell.SpecialFolders
property, used to work with file folders
WshUnnamed
Accessed through the WScript.Arguments
.Unnamed
property, allows you to work with
unnamed arguments passed to a script
WshUrlShortcut
Accessed through the WshShell.CreateShortcut
method, used to create and manage URL shortcuts
86804c01.indd 7
86804c01.indd 7
1/21/09 1:16:18 PM
1/21/09 1:16:18 PM
8
Part
I
Getting Started with Windows Scripting
With the JScript scripting engine, the letter case for object, method, and property names
is important. The JScript engine doesn’t recognize an object unless you reference it prop-
erly. For example, with WScript , the JScript engine does not recognize Wscript . Because VBScript
really doesn’t care about letter case, either Wscript or WScript works just fi ne.
More on scripting hosts
To execute Windows scripts, you’ll use one of the two scripting hosts available, either WScript or
CScript. WScript has GUI controls for displaying output in pop-up dialog boxes and is used pri-
marily when you execute scripts from the desktop. CScript is the command-line executable for the
scripting host that is used when you execute scripts from the command line. Although you can work
with both of these hosts in much the same way, there are some features specific to each, which we
discuss later in Chapter 4. For now, let’s focus on how the scripting hosts work.
Several file extensions are mapped for use with the scripting hosts. These file extensions are:
.js: Designates scripts written in JScript
■
.vbs: Designates scripts written in VBScript
■
.wsf: Designates a Windows script file
■
.wsh: Designates a WSH properties file
■
A limitation of .js and .vbs files is that they can contain only JScript or VBScript statements, respec-
tively, and you cannot mix and match. This is where .wsf files come into the picture. You can use
.wsf files to create WSH jobs, or what I call batch scripts. These batch scripts can combine multiple
types of scripts and can also include type libraries containing constants.
Batch scripts contain markup tags that identify elements within the batch, such as individual jobs
and the scripting language being used. These markup tags are defined as XML (Extensible Markup
Language) elements. XML is structured much like HTML and uses plain-text characters. You can
use any text editor to create batch scripts and, because batch scripts contain XML, you can also use
an XML editor.
Windows scripts can also use .wsh files. These files contain default settings for scripts, such as
timeout values and script paths. Because of the introduction of .wsf files and direct in-script sup-
port for most script properties, .wsh files are rarely needed.
More on scripting engines
Scripting engines provide the core language functionality for Windows scripts and are packaged
separately from the Windows Script Host itself. You can obtain scripting engines for JScript, VBScript,
Perl, TCL, Python, and more. The official Microsoft scripting engines for VBScript and JScript are
standard components on Windows and are the focus of this book.
With Windows scripting, many of the features available for scripting with Internet Explorer and
the Web aren’t available. Functions needed for Web scripting simply aren’t needed for Windows
NOTE
NOTE
86804c01.indd 8
86804c01.indd 8
1/21/09 1:16:18 PM
1/21/09 1:16:18 PM
9
Introducing Windows Scripting
1
scripting and vice versa. For example, in JScript, none of the window-related objects are available in
WSH because, in Windows, you normally don’t need to access documents, forms, frames, applets,
plug-ins, or any of those other browser-related features. The exception to this is if you create a script
that starts a browser session; within the browser session, you can use the browser-related objects as
much as you want.
Right now, you may be wondering what exactly is and isn’t supported by Windows scripts. In a
nutshell, the scripting engines support core language and language runtime environments. The core
language includes operators, statements, built-in objects, and built-in functions. Operators are used
to perform arithmetic, comparisons, and more. Statements are used to make assignments, to condi-
tionally execute code, and to control the flow within a script. For example, you can use
for
looping
to execute a section of code for a specific count. These types of statements are all defined in the core
language. Beyond this, the core language also defines the core functions and objects that perform
common operations such as evaluating expressions, manipulating strings, and managing data.
The runtime environment adds objects to the core object model. These objects are used to work with
the operating system and are available only with Windows Scripting. Table 1-2 provides a complete
list of the available VBScript objects. The list is organized according to where the objects originate,
either in the runtime environment or the core object model.
TABLE 1-2
VBScript Objects for Windows Scripting
Runtime Objects
Core Objects
Dictionary
object
Class
object
Drive
object
Debug
object
Drives
collection
Dictionary
object
File
object
Err
object
Files
collection
FileSystemObject
object
FileSystemObject
object
Match
object
Folder
object
Matches
collection
Folders
collection
RegExp
object
TextStream
object
SubMatches
collection
Table 1-3 provides a complete list of available JScript objects. Again, the list is organized according
to where the objects originate.
86804c01.indd 9
86804c01.indd 9
1/21/09 1:16:18 PM
1/21/09 1:16:18 PM
Dostları ilə paylaş: |