Windows 7 and Vista guide to scripting, automation, and ...

Windows 7 and Vista Guide to

Scripting,

Automation,

and Command Line Tools

Brian Knittel

PUG

800 East 96th Street Indianapolis, IN 46240 USA

Table of Contents

Introduction 1

I Scripting with "Windows Script Host

1 Windows Script Host 9

What Is a Windows Script? 9 The "Script" Part 9

The "Windows" Part 10 The "Host" Part 11

How Is This Different from Writing Batch Files? Scripting Languages 13

VBScript 14 JScript 14 Perl 15 Python 15 Open Object REXX 15 Ruby 15 Choosing a Language 16 A Simple Script 16 Types of Script Files 19 JSE andVBE: Encoded Scripts 20 Windows Script Files (WSF) 21 Windows Script Components (WSC) 23 WSH Settings 23 Creating Your First Script File 24 Making and Securing a Script Folder 24 Creating a Script 26 Script Editing Tools 27 How Windows Runs Scripts 28 Wscript and Cscript 28 Ways to Run a Script 29 Passing Information to Scripts 31 Saving the Results from Scripts 32 Wscript and Cscript Command Options 33

Running Your Own Scripts 36 Adding Scripts to the Path 37 Running Scripts with a Shortcut Icon 38 Making a Script Shortcut 39 Running Scripts from Batch Files 39 Running Scripts Automatically 40

Security Concerns 40 Trust Policy and Script Signing 42

Debugging Scripts 42

Where to Get More Information 47

2 VBScript Tutorial 49

Introduction to VBScript 49

Variables 50 Constants 51 Named Constants 52

Operators and Expressions 53

Automatic Conversion 57 Flow Control 57

The If...Then Statement 58 The Select Case Statement 61

The Do While Loop 63 Terminating a Loop with Exit Do 65 Counting with the For...Next Statement 66 Processing Collections and Arrays with For...Each VBScript Functions 68 Calling Functions and Subroutines 69 Documentation and Syntax 70 String-Manipulation Functions 71

Date and Time Functions 74

Interacting with the User 79 The MsgBox() Function 79 The InputBox() Function 82 Printing Simple Text Messages with Wscript. Echo

Advanced VBScript Topics 85 Error Handling 86 Procedures: Functions and Subroutines 87

Arrays 89 Variable Scope 91 Where to Go from Here 92

3 Scripting and Objects 93 Introduction to Objects 93

Classes and Instances 94 Containers and Collections 95 Object Naming 97

Using Objects with VBScript 98

Automation and Document Files 99 The Difference Between Properties and Methods Nested Objects 101 Releasing Objects 102 Working with Collections 102

Using Objects with JScript 104 Case Sensitivity 104 Working with Collections 104

Using Objects with ActivePerl 106 Running Perl Scripts in WSH 106 The Perl Object Interface 107 Working with Collections 108

Using Objects with ActivePython 109 Working with Collections 110

Using the WScnipt Object 111 Retrieving Command-Line Arguments 113

Locating and Using Unusual Objects 115

4 File and Registry Access 123 Getting Real Work Done 123 Manipulating Files and Folders 124

Scripting.FileSystemObject 124

Working with File and Pathnames 130 The Scripting.Drive Object 135 The Scripting.Folder Object 139 The Scripting.File Object 144

Reading and Writing Files 149 The TextStream Object 150 Reading Text from Files 152 Writing Text to Files 154 Working with Stdin and Stdout 159 Reading Binary Files 163

Reading and Writing XML 167

Some XML Basics 168 Reading an XML File 176 Creating an XML or HTML File 179 Manipulating Programs and Shortcuts 181 The WScript.Shell Object 182

Running Programs 186 Creating and Modifying Shortcuts 193 Working with the Environment 196 Extracting Environment Information 198 Managing Environment Settings 199 Working with the Registry 201 Examining Registry Keys and Values 202 Saving Information in the Registry 203

5 Network and Printer Objects 207 Managing Network and Printer Connections 207 Retrieving Network User Information 212 Managing Drive Mappings 214 Listing Drive Mappings with EnumNetworkDrives Adding Drive Mappings 218 Deleting Drive Mappings 219 Setting Up Mappings in a Script 220 Managing Network Printer Connections 221 Displaying Printer Information 222 Connecting to Network Printers 223 Redirecting DOS Session Printers 225 Deleting Printer Connections 226 Setting the Default Printer 228 Printing from Scripts 229

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download