sams teach Yourself windows Script Host in 21 Days phần 1 ppt

51 646 1
sams teach Yourself windows Script Host in 21 Days phần 1 ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Teach Yourself Windows Script Host in 21 Days Thomas Fredell With Michael Morrison, Stephen Campbell, Ian Morrish, and Charles Williams Acquisitions Editor Dustin Sullivan Development Editor Tom Dinse Managing Editor Lisa Wilson Project Editor Sara Bosin Copy Editor Kelly Talbot Indexer Becky Hornyak Proofreader Benjamin Berg Technical Editor Eric Richardson Interior Design Gary Adair Cover Design Aren Howell Copy Writer Eric Bogert Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Layout Technicians Ayanna Lacey Heather Miller Copyright © 1999 by Sams Publishing All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. International Standard Book Number: 0-672-31374-x First Printing: July 1999 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Windows, NT, and Windows 2000 are registered trademarks of the Micr osoft Corporation. Introduction "Damn those batch files!" "I can't stand batch files!" If you're an experienced Microsoft DOS or Windows user, you've probably cursed batch files at one time or another. Batch files enable you to automate tasks that you perform repetitively, but they are pretty weak and relatively unstructured. It can also be difficult to debug them and get them to work correctly, and they have some strict limitations that severely limit their capabilities. The Windows Script Host (WSH) has come to the rescue of Windows users that are held hostage by crummy batch files. The Windows platform includes a very powerful scripting solution that enables you to do everything that is possible with batch files—plus much, much more! They are incredibly useful no matter what your needs are—whether you're an administrator, a power user, or a software developer. Note By the way, you'll notice that throughout this book, Windows Script Host is referred to as Windows Scripting Host . Microsoft decided to change the name while this book was being written, and although the title was changed to reflect that, we didn't go back and change every occurrence in the book. WSH is an intrinsic part of Microsoft Windows 98, Windows 2000, and Windows NT 5.0. For earlier versions such as Windows NT 4.0 or Windows 95, you can install a free operating system add-in from Microsoft. When you have WSH running on your computer, you have the capability to run scripts that use the Microsoft VBScript or JScript languages to automate your tasks. WSH is also flexible enough to support other Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com languages, thanks to its modular architecture. Furthermore, WSH enables you to automate practically everything in Windows because it’s plugged in to the standard OLE/ActiveX capabilities. The goal of this book is to teach you everything about WSH and all its uses. I think that we’ve come pretty close to the first goal, but we can’t possibly reach the second because WSH is so flexible it can do almost anything! Instead, we decided lay out a ton of information about WSH, and then discuss ways that you can use it to automate a wide variety of tasks—everything from working with files to administering Microsoft SQL server and the Microsoft IIS Web server. Hopefully, when you've finished this book, you'll have the capability to see how WSH technology can be applied creatively to solve your unique challenges. How to Use This Book This book is intended to be a standalone, comprehensive guide for the Windows Script Host technology. It lays a foundation for understanding WSH by discussing the roots of the technology and by looking at some basic examples. Then it provides a reference for the objects and languages that are part of WSH. Finally, it moves into very detailed examples of how you can use WSH for many different tasks. Along the way, we also discuss the tools that you can use to build WSH scripts, techniques for debugging WSH scripts, and steps for deploying the scripts that you write. In short, if you want one comprehensive guide to learning and using WSH, this book is it! It covers WSH from soup to nuts, and lays a strong foundation so that you can leverage WSH for your own needs. It also provides standard functions for many tasks that will make it easier for you to quickly leverage WSH. This book is a great book no matter what type of user you are; if you're an administrator, you'll be intrigued by the ways that we use WSH to script user logins and to perform server administration for software such as Microsoft IIS and SQL Server. If you're a power user, you'll find that it is a useful tutorial and reference for software, and that you can use it to make your life easier by automating many of the tasks that you do manually. Finally, if you're a developer, you'll love the detailed descriptions and examples of how you can build your own components to integrate with WSH. How This Book Is Organized Sams Teach Yourself Windows Script Host in 21 Days covers the Windows Scripting Host technology in depth. For each of the 21 days, there's a corresponding chapter. Following is a rough breakdown of the chapters and the categories into which they fit. Introduction to WSH Day 1, "Introduction to the Windows Script Host (WSH)," is WSH 101. You learn about the types of technologies that have been used for automation, such as batch files, and how WSH fits in to the picture. You also get an overview of the components that are used with WSH. Day 2, "Writing a Simple Script Using WSH," jumps right in and shows you how you can begin to create WSH scripts. It describes the tools that are used to write scripts, from the Windows Notepad to more powerful editors, and gives you a step-by-step process with a detailed example that shows you how to create your scripts. Day 3, "The Scripting Object Model," provides a comprehensive reference, with examples, for the objects that are provided by the scripting engine that is used by WSH. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Day 4, "The WSH Object Model," gives you a comprehensive reference, with examples, for the objects that are provided by WSH. Day 5, "Using Microsoft VBScript with WSH," gives you a reference for the VBScript language that can be used with WSH. Day 6, "Using Microsoft JScript with WSH," gives you a reference for the JScript language that can be used with WSH. Running and Debugging WSH Scripts Day 7, "Running and Scheduling Scripts," shows you how you can run and schedule scripts to automate repetitive tasks. Day 8, "Testing and Debugging WSH Scripts," shows you practical techniques that you can use to test and debug your scripts to ensure that they run correctly. Performing Administrative Tasks Using WSH Day 9, "Handling WSH Script Arguments and Reading/Writing Files," gives you some standard techniques that you can use over and over again in your scripts. Day 10, "Replacing Batch Files with WSH," shows you how you can begin to eliminate your batch files and replace them with more robust WSH scripts. Day 11, "Administering User Logins, Program Settings, Folders, and File Shortcuts Using WSH," will help you to understand how you can use WSH to control users, programs, folders, and files. Day 12, "Automating Microsoft SQL Server Administration Using WSH Scripts," shows you how you can leverage the SQL Server Distributed Management Objects to automate your SQL Server administration. Day 13, "Automating Microsoft IIS Administration Using WSH Scripts," will help you to create scripts that enable you to quickly and easily administer the Microsoft IIS Web server. Scripting Applications and Accessing Databases Using WSH Day 14, "Overview of WSH and Application Scripting Capabilities," describes how you can use WSH to script any application that provides interfaces through OLE Automation/ActiveX. Day 15, "Scripting the Microsoft Office Suite," shows you how you can use WSH scripts to control and automate Microsoft Office applications such as Word and Excel. Day 16, "Using WSH to Access Databases Through Microsoft Active Data Objects (ADO)," offers an introduction to the Microsoft Active Data Objects and a reference that will help you to use ADOs to access databases from your WSH scripts. Day 17, "Using ADO for WSH Scripting Tasks," shows you how to use ADO objects to integrate databases with your WSH scripts. Day 18, "Using WSH to Manipulate the Microsoft Active Directory," will help you use WSH scripts to control the Microsoft Active Directory. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Extending and Deploying WSH Scripts Day 19, "Using Scripting for Messaging," touches on using Active Directory to manage Microsoft Exchange server and goes into more depth on Collaborative Data Objects (CDO), which are Microsoft’s objects for accessing any messaging system. Day 20, "Using WSH with Custom VB and Java Objects," shows you how to create new objects, using Visual Basic and Java, that you can use to enhance your WSH scripts. Day 21, "Deploying Script Solutions," will help you transfer your scripts to other workstations or servers so that you can leverage the scripts you write throughout your organization. Special Features in This Book This book has some special features that are designed to help you get the information you need—fast. These features are described in this section. Sidebars Contain Interesting Nuggets of Information Sidebars are detours from the main text. They usually provide background or interesting information that is relevant, but not essential reading. You might find information that's a bit more technical than the surrounding text, or you might find a brief diversion into the historical aspects of the text. Note Notes provide additional information that is related to the text. They provide you with useful tidbits of information. Caution Cautions alert you to problems that you might encounter when you are using parts of the technology. Conventions In addition to the special features that help you find what you need, this book uses some special conventions that make it easier to understand. The following sections describe the keyboard, mouse, typographical, and other conventions found in this book. Keyboard The keyboard conventions that are listed here help you to better understand what the instructions are telling you to do. For example, they help you understand the key combinations and menu commands that you are type or choose: Element Convention Hot keys Hot keys are underlined in this book, just as they appear on Windows menus. For example, the F in F ile is a hot key. To use a hot key, press Alt and the underlined letter to make that menu appear. Key combinations Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com example, "Press Ctrl+Alt+D" means that you press and hold down the Ctrl and Alt keys simultaneously and then press and release the D key. Always press and release, rather than hold down, the last key in a key combination. Menu commands A comma is used to separate the parts of a pull-down menu command. For example, if you are told to "Choose F ile, New," you need to open the File menu and select the N ew option. In most cases, special-purpose keys are referred to by the text that actually appears on them on a standard 101-key keyboard. For example, you’ll see "Press Esc," "Press F1," or "Press Enter." However, some of the keys on your keyboard don’t have words on them. Following are the conventions that are used for those keys: Backspace key Up-, down-, left-, or right-arrow key Mouse In this book, the following phrases tell you how to operate the mouse within Windows: Click Double-click Drag Drop Typeface This book uses some special typeface conventions that make it easier to read: Element Convention Italic Italic indicates new terms. It also indicates placeholders (words that stand for what you actually type) in commands, addresses, and code listings. Bold Bold is used for menu commands and options that appear in dialog boxes, as well as for URLs. Computer font Computer font is used for text you see onscreen, code listings, and any text you’re asked to type. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Week 1: Days 1 to 7 Day List Day 1: Introduction to the Windows Script Host (WSH) Day 2: Writing a Simple Script Using WSH Day 3: The Scripting Object Model Day 4: The WSH Object Model Day 5: Using Microsoft VBScript with WSH Day 6: Using Microsoft JScript with WSH Day 7: Running and Scheduling Scripts Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Day 1: Introduction to the Windows Script Host (WSH) Overview The Windows Scripting Host (WSH) is an incredible enhancement to the Microsoft Windows series of operating systems. Finally, Windows users have equivalent scripting functionality to what UNIX users have had for years! On the first day of your journey to become an expert WSH user, you’ll do the following: • Get an introduction to the new Microsoft scripting technology • Learn about how WSH relates to other scripting technologies • Learn about the major components of WSH Introduction to the Windows Script Host (WSH) Microsoft has finally fixed a problem that has plagued the MS-DOS and Windows operating systems for years. Until recently, neither MS-DOS nor Windows has included a powerful scripting tool. Anyone who has tried to automate standard tasks on a PC running Windows or MS-DOS knows that it isn't easy. At best, you can throw together a couple of commands in a DOS batch file, but you don't have any real control over what happens: There's no looping, poor variable support, painfully simple logical statements, and limited error-handling support. UNIX users have been thumbing their noses about this issue for years! The solution to the problem is the Microsoft Windows Scripting Host (WSH). WSH leverages technologies that Microsoft developed to perform scripting on the Web; you'll learn more about how it relates to other Microsoft Internet technologies later in this chapter. WSH provides the capability to develop scripts using the Microsoft scripting languages VBScript and JScript (which is very similar to the popular JavaScript language). Like DOS batch files, you can run them from the command line. Unlike DOS batch files, they have all the power of the programming language that you choose; for example, if you're using VBScript, you have the capability to define variables, use loops, and do anything that you would expect from a robust language. Additionally, you have the capability to access and control ActiveX objects. That means that the capabilities of WSH for automation are almost limitless! Figure 1.1 shows you the output from the simple WSH script in Listing 1.1. Figure 1.1: The output from a simple WSH script. Listing 1.1 A Simple WSH Script Dim count Wscript.Echo "Windows Scripting Host is as easy as " Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Wscript.Echo "" For count = 1 to 3 Wscript.Echo count Next Note You will have trouble running this script from the command line if you don’t already have the WSH components installed. If you have Windows 98 or are running Windows NT 4.0 with the NT option pack, you already have WSH. Otherwise, you need to install the WSH components. You can get them off of the Microsoft Scripting Web site at http://www.microsoft.com/scripting . In this book, you’ll learn about the underpinnings of the Windows Scripting Host. You’ll find out how it works, you’ll learn about the VBScript and JScript languages that can be used with it, and you’ll see useful examples that you can leverage to automate tasks that you might need to accomplish as a user, programmer, or system administrator. Background: Automating Tasks The moment that you type the same command twice at the command line, or heaven forbid, a string of commands, you realize that it would be great to be able to capture the commands that you entered so that you can run them again without having to retype all of them. You record your commands in some type of command or script file. If you use a word processor such as Microsoft Word or a spreadsheet a la Excel, you might have automated some of your common tasks using macros. Macros are conceptually similar to scripts; the difference is that they run within an application environment such as Word or Excel. After you’ve done that, you might find that the commands are good, but a little inflexible. Sometimes you might want them to do things a little differently; for example, you might not want to hard-code the name of a file that your commands process. Instead, you might prefer to be able to tell the script what file to process. You implement parameters for your script. If you’re an experienced computer user, this probably sounds like an experience that you might have had first-hand. If you haven’t had this experience, you have a lot to gain from this book because it will show you how you can begin to automate things that you might be doing repetitively today. Your capability to automate tasks and to create command or script files depends to some degree on the type of operating system that you’re running. If you’re a Windows user, you have the option of using DOS batch files or WSH scripts. If you’re a UNIX or Linux user, you can use common UNIX scripting languages such as Perl or TCL. To put these scripting solutions into context, and so that you understand the background for WSH, a little description of each is given in the following sections. DOS Batch Files DOS batch files were the original command scripting tools for the MS-DOS operating system. As the Windows shell emerged, DOS batch files were carried forward as the inherent scripting technology. Even with the version of Windows that finally left DOS behind, Microsoft Windows NT, batch files remained the only scripting choice provided by the operating system. Batch files are extremely simple text files that contain sets of commands. Some commands are inherent in the batch language; for example, the IF command is u sed for Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com logical processing. From a batch file, you can also call or use any other program in the system. For example, you can use the sort program to sort the contents of a file alphabetically. (The sort program is a standard DOS-style utility that is provided with Microsoft Windows.) Figure 1.2 shows a sample batch file that takes a text file as input and displays the contents sorted alphabetically. Figure 1.2: An example of a DOS batch file. The benefit of DOS batch files is that they provide a simple, easily understandable language for command scripting. At the most basic level of functionality, it doesn’t take a whole lot of knowledge to slap together a few commands in a single file. However, there are a lot of problems with batch files. If you need to do an iterative process, batch files can be extremely difficult to use. Why? Because they have no intrinsic looping mechanism. About the best that you can do is have the batch file call itself, and use some flags to make sure that you break out of the call-loop at some point. And if you want to do a specific number of iterations, such as performing some task x times, forget about it! There might be a way to kludge that type of functionality in a batch file, but if there is, it would be complex enough that no one would want to maintain it! DOS batch files are a simple method of command scripting, but they suffer from a number of serious limitations. Looping is one of the easiest limitations to point out, but there are many others as well (such as limited variable handling, no inherent regular expression tools, and no capability to create self-contained functions or procedures). Certainly, compared to UNIX scripting languages, batch files are a weak, kludgey solution to a common problem. Thankfully, they’ve been supplemented by the Windows Scripting Host, which you’ll read about later in this chapter! UNIX Scripting The various flavors of the UNIX operating system provide robust scripting capabilities. The UNIX system shell, which is basically the equivalent of the command prompt in Windows, enables you to define shell scripts that automate tasks. Several languages are available for scripting; two of the most popular are Perl and TCL. Unlike DOS batch files, Perl and TCL are languages that include all the functionality that you would expect from a full programming language. Using variables, implementing loops, and creating functions or procedures are simple using either language. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... Excel In Chapter 15 , "Scripting the Microsoft Office Suite," you’ll see how you can use WSH to script the Microsoft Office suite.à à The entire foundation of the Windows Scripting Host is based on ActiveX technology The Windows Scripting Host defines interfaces that are implemented by scripting engines Consequently, the same scripting engine can be used in any application that can use the scripting interface... WSHà à The Windows Scripting Host (WSH) is a new solution to the problem of command scripting for the Windows operating systems WSH enables you to develop command scripts using a scripting engine It provides two scripting engines: the Microsoft VBScript and JScript scripting engines VBScript is a language that’s very similar to Microsoft Visual Basic, and JScript is very similar to the JavaScript language,... Summaryà à The Windows Scripting Host is an exceptionally powerful solution for command scripting in the Microsoft Windows environment No longer are Windows users neglected when it comes to automation control over the Windows environment Relative to other operating systems with powerful scripting capabilities, Microsoft has made up lots of ground with the introduction of the Windows Scripting Host Windows. .. can either use the command-line cscript program or the Windows- interface wscript program When you use either, it finds the appropriate scripting engine to interpret your script by examining the Windows Registry The following steps are performed by WSH to find and instantiate the appropriate scripting engine component:à à 1. à It searches the HKEY_CLASSES_ROOT Registry key to find an entry that corresponds... program to install WSH on your system.à à à Simpo 2: Merge and a Simple Script Using WSH Day PDFWriting Split Unregistered Version - http://www.simpopdf.com à à à Overviewà à At this point, you should have a reasonable understanding of the technology that underlies the Windows Scripting Host During Day 1, "Introduction to the Windows Scripting Host (WSH)," you learned how the Windows Scripting Host works... add-on script interpreters, WSH is becoming an integral part of the Microsoft Windows operating system, so you’ll eventually be able to use it immediately in any Windows environment.à The Windows Scripting Host (WSH) enables you to execute scripts directly from the Windows desktop or the command prompt WSH uses the same scripting engines that are used by Microsoft Internet Explorer for client-side scripts... When WSH runs a script, it first instantiates the scripting engine by creating the COM component that implements the scripting language Then, it passes the text of the script to the scripting engine by calling the IActiveScriptParse::ParseScriptText() method The scripting engine is responsible for parsing and executing the text of the script. à à There are two ways for you to run a script using WSH: You... component by calling the Windows COM API CoCreateInstance() function The API call causes the Windows ActiveX libraries à to instantiate an instance of the scripting object The result of à CoCreateInstance(), assuming that the call is successful, is a pointer to the IUnknown interface of the scripting engine.à à Ãà 5.à à Finally, WSH retrieves a pointer to the IActiveScriptParse interface of the à à interact... the installation of some of their technologies; for example, the Windows NT 4.0 option pack, which includes the Microsoft Transaction Server, includes VBScript script files to install or uninstall MTS examples.à à WSH and Scripting Enginesà à WSH scripting engines are COM components that support the IActiveScriptParse COM interface Because Microsoft has defined the interface between the scripting engine... interact with the scripting engine.à Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com à à à Running Scripts Using WSHà à As indicated in the previous section, you have two options to use WSH to run a script You can either run a script using the command-line or console WSH scripting utility (cscript), or using the Windows interface version of the utility (wscript).à à The cscript utility . http://www.simpopdf.com Day 1: Introduction to the Windows Script Host (WSH) Overview The Windows Scripting Host (WSH) is an incredible enhancement to the Microsoft Windows series of operating systems. Finally, Windows. Figure 1. 1 shows you the output from the simple WSH script in Listing 1. 1. Figure 1. 1: The output from a simple WSH script. Listing 1. 1 A Simple WSH Script Dim count Wscript.Echo " ;Windows Scripting. The Windows Scripting Host defines interfaces that are implemented by scripting engines. Consequently, the same scripting engine can be used in any application that can use the scripting interface

Ngày đăng: 13/08/2014, 08:21

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan