Windows Script Host
Authors: Tim Hill
Venue:  (1999)

From the Book:This is a book about system scripting for Windows 2000 and Windows NT A system script is a procedure or set of procedures used to automate or Simplify an operating system related task. The task might be as simple as copying a single file or as complex as monitoring a set of enterprise servers. Generally, the more complex the task being scripted, the more useful the script because it avoids the need to manually repeat a series of operations, which can be both monotonous and error-prone. A Short History of Scripting From their earliest days, operating systems have typically incorporated some sort of command shell or command interpreter. This shell allows an interactive user to enter text commands and see the results as textual output. Interaction with these shells always follows the same basic pattern: 1. The operating system prompts the user to enter a command. 2. The user types a line of text (the command) and presses Enter (or some other command termination key). 3. The operating system executes the command and (typically) displays the results of this processing. By entering sequences of these simple commands, an operator can control all aspects of operating system and computer operation. Operating system designers were quick to notice that many systemsrelated tasks involve the repeated execution of specific sequences of commands. To increase efficiency and reduce errors, a system was devised to allow these command sequences to be stored in a file ready for later execution. Upon demand, the operating system reads the file and executes the commands it contains. These simple command lists are called shell scripts, batch files, or batch jobs. Batchfiles have been significantly enhanced over the years. One of the first enhancements was parameterization, which allows the batch file to be supplied with arguments when it is executed. In addition, various controlflow techniques were borrowed from traditional programming languages. Eventually, batch files reached the point where they could be considered simple programming languages in their own right. This evolution, however, has led to some significant problems. For example, in my book, Windows NT Shell Scripting (by Macmillan Technical Publishing), I described Windows NT batch scripts in detail, including the many script enhancements provided in the Windows NT command shell (CMD.EXE). However, these batch scripts are (at best) awkward to use, and the syntax of the scripts is cumbersome. More importantly, batch scripts are unable to interact with Windows applications to any great extent, which excludes these scripts from many possible uses. Finally, the enhanced Windows NT batch scripts are not cross-platform, and they cannot be run on the Windows 9S and 98 platforms. These shortcomings have led Microsoft to devise a new set of script technologies that build not upon traditional command-line concepts, but upon modern object-oriented software techniques. These new technologies, collectively referred to here as system scripting, are the subject of this book. Who Should Read This Book Virtually anyone who wishes to automate a repetitive or awkward operating system task will benefit from the information in this book. However, this book is aimed primarily at Windows 2000 and Windows NT professionals, systems administrators, and power users. Thus, most of the examples and sample scripts deal with system administration and management issues. However, the techniques presented all have much wider applicability, and there is really no limit to the uses for system scripts. To illustrate this, the final sample script in this book presents a simple interactive game. One area that can be daunting to some potential users of system scripting is that, for the first time, the core of the script system is a true computer language. Previously, Windows NT batch files could be viewed as an extended form of a macro, and therefore, they were just a substitution for directly typing in commands. With system scripting this is no longer the case, which means that many users may be faced with the need to learn a full programming language for the first time. To ease the trepidation this can cause, I have included in this book a set of sidebars under the heading "Program Guide," which are intended to guide the beginner into the world of software programming with as little pain as possible. About ActiveX Scripting Technology This book describes several new system scripting technologies that are available for the Windows 2000 and Windows NT operating systems (as well as Windows 95 and 98). These are: Windows Script Host (WSH) As a full system-scripting environment, WSH is designed (and almost certainly destined) to replace the old batch scripts that Windows 2000 and Windows NT inherited from MS-DOS. WSH sweeps away the arcane syntax and complexity of these scripts and replaces them with a clean, easy-to-use script environment and a simple, orthogonal object model for accessing application and system functionality. Visual Basic Script Edition (VBScript) The VBScript language is derived from the popular Visual Basic language, itself a (distant) derivative of the original Dartmouth BASIC developed in the 1970s. The use of VBScript as the core language allows system scripts to take advantage of the robustness and versatility of a modern structured programming language. COM Objects VBScript has constructs to allow the direct manipulation of COM Objects that expose an automation interface. This allows VBScript to directly control and leverage the object model built into many modern applications, such as Microsoft Office, and system technologies, such as ADSI. Windows Script Components Windows Script Components (previously known as Server Scriptlets) provide a means to construct COM Objects entirely in script. As such, Windows Script Components are often used to encapsulate reusable script code, which is then referenced from within one or more WSH scripts. These technologies offer Windows 2000 and Windows NT professionals unprecedented opportunities to automate and control all aspects of client and server computer operations. This book provides all the information needed to effectively use and deploy these technologies on everything from a stand-alone computer to a full enterprise network. The foundation for all of these new technologies is an architecture known as ActiveX Scripting. The ActiveX Scripting architecture, shown in Figure 1.1, provides a standard way to separate script hosts from script engines. The script engines provide the actual script language, whereas the hosts provide an interface between the language and whatever application or facility you plan to script. Because the ActiveX Scripting architecture provides a standard way to connect hosts to engines, any engine can run with any host. Thus, for example, if you obtain a Perl ActiveX Scripting engine and install it, all ActiveX Scripting hosts will automatically be able to execute Perl scripts. Examples of ActiveX Scripting hosts include the popular Internet Explorer web browser, the Microsoft Internet Information Server web server, and the Windows Script Components and WSH technologies described in this book. Examples of ActiveX Scripting engines include the VBScript and JScript languages. There are also third party ActiveX Scripting engine versions of other popular languages, including Perl and Tcl. This book covers only the VBScript language because this language is in many ways more approachable for newcomers to programming. In addition, the popularity of Visual Basic means that many people will already be familiar with the underlying syntax of VBScript, based as it is upon this language. However, if you prefer to use another language, then you are free to do so-that is one of the advantages of the ActiveX Scripting architecture. However, if you do use a language other than VBScript or JScript, you will have to ensure that the language engine is installed on all machines where your scripts are to be executed. Organization of This Book This book is divided into three parts and several appendices. Part I, "WSH and the VBScript Language" The first part of the book covers two topics: WSH and the VBScript language (with the exception of objects and classes, which are described in Part II, "Objects and Classes"). As such, the information in this part forms the foundation for using system-scripting technologies. If you are already familiar with VBScript, you might wish to scan the information on WSH briefly and then skip ahead to Part 11. If you have not used VBScript before or if you are unfamiliar with computer programming, the material in Part I will provide you with all the core concepts needed to use the VBScript language effectively. Part 11, "Objects and Classes" Part 11 of the book covers objects and classes, and it explains the objectoriented paradigm and how this paradigm is used in system scripting. The focus of this part of the book is on the objects and features that are built-in to either WSH or VBScript. You will also find information here on how to construct your own "in-script" objects using VBScript. Part 11 also shows how to use COM Objects. Many technologies and applications expose their functionality through COM Objects, and therefore, understanding how to access these objects is critical to using system scripting effectively. Finally, Part II of the book also describes Windows Script Components, which is a technology that allows you to create your own COM Objects using VBScript. Windows Script Components provides an excellent way to break up large projects into smaller sub-projects and provides the foundations for dynamic script re-use. Part III, "Sample Script Solutions" Part III of the book builds upon the material in the earlier two parts to provide a library of complete, ready-to-use sample scripts. These scripts can be used "as-is" or as a starting point for a custom script project. Part IV, "Appendices" Finally, the appendices at the end of the book provide additional informa- tion that might be useful to system script authors. Appendix A, "Installing Script Components," provides installation instructions for the system scripting components described in this book. Appendix B "Other Scripting Resources," provides information on other script resources available via the Internet. Conventions Used in This Book Throughout this book, VBScript source code is shown in a monospace font using a mixed-case convention. However, the VBScript language is not casesensitive, and source code can be entered as desired. This may not be true of third party ActiveX Scripting languages-consult the specific language documentation for information on case sensitivity. When describing VBScript and other syntax (instead of showing an example), items that must be entered exactly are shown in mixed case. Placeholders that must be replaced by appropriate values are shown in lower case italic. Optional items are shown in brackets, and vertical bar characters separate alternatives, where one item from a list must be selected. For example, the Do command syntax is: Do [While expression] This example shows that the Do command can be optionally followed by a While clause, which consists of the keyword While followed by an expression. Some sample scripts in the book are shown with accompanying line numbers. These line numbers are for reference purposes only-scripts do not include line numbers in the program text. Before You Continue Before continuing, you might want to take a few minutes to install the latest system scripting components on your computer. Appendix A provides details on where to obtain these components and how to install them. You might also want to look at Appendix B, which provides some links to additional scripting resources, tools, and technologies. The information in this book is based upon versions 1.0 and 2.0 of Windows Scripting. The VBScript information is based upon version 5.0 of the VBScript engine. Refer to Appendix A for information on obtaining the most recent versions of all these components.