logo资料库

Windows-7-and-Vista-Guide-to-Scripting,Automation and Command Li....pdf

第1页 / 共896页
第2页 / 共896页
第3页 / 共896页
第4页 / 共896页
第5页 / 共896页
第6页 / 共896页
第7页 / 共896页
第8页 / 共896页
资料共896页,剩余部分请下载后查看
Cover
Copyright
Contents at a Glance
Table of Contents
Introduction
I: Scripting with Windows Script Host
1 Windows Script Host
What Is a Windows Script?
Scripting Languages
A Simple Script
Types of Script Files
Creating Your First Script File
How Windows Runs Scripts
Running Your Own Scripts
Security Concerns
Debugging Scripts
Where to Get More Information
2 VBScript Tutorial
Introduction to VBScript
Flow Control
VBScript Functions
Interacting with the User
Advanced VBScript Topics
Where to Go from Here
3 Scripting and Objects
Introduction to Objects
Using Objects with VBScript
Using Objects with JScript
Using Objects with ActivePerl
Using Objects with ActivePython
Using the WScript Object
Locating and Using Unusual Objects
4 File and Registry Access
Getting Real Work Done
Manipulating Files and Folders
Reading and Writing Files
Reading and Writing XML
Manipulating Programs and Shortcuts
Working with the Environment
Working with the Registry
5 Network and Printer Objects
Managing Network and Printer Connections
Retrieving Network User Information
Managing Drive Mappings
Managing Network Printer Connections
Printing from Scripts
6 Messaging and Faxing Objects
Sending Email from Scripts with CDO
The CDO Object Model
Sending a Message with CDO
Faxing from Scripts
7 Windows Management Instrumentation
Introduction to Windows Management Instrumentation
Making WMI Connections
WMI Collections and Queries
Scriptomatic
WMI Examples
For More Information
8 Active Directory Scripting Interface
Managing the User Directory
ADSI Concepts
ADSI Objects for the WinNT: Provider
IIS and Exchange
Managing Active Directory
Active Directory Objects
Developing ADSI Scripts
EzAD Scriptomatic
For More Information
9 Deploying Scripts for Computer and Network Management
Using Scripts in the Real World
Using WSF Files
Deploying Scripts on a Network
Creating Simple Installation Programs with IExpress
Writing Scripts to Manage Other Computers
Scripting Security Issues
Setting Up Logon Scripts
Scheduling Scripts to Run Automatically
II: The Command Line Environment
10 The CMD Command-Line
The Command Prompt
Running CMD
Command-Line Processing
Configuring the CMD Program
Built-in Commands
Getting More Information
11 Batch Files for Fun and Profit
Why Batch Files?
Creating and Using Batch Files
Batch File Programming
Displaying Information in Batch Files
Argument Substitution
Argument Editing
Conditional Processing with If
Processing Multiple Arguments
Working with Environment Variables
Processing Multiple Items with the for Command
Using Batch File Subroutines
Prompting for Input
Useful Batch File Techniques
12 The MS-DOS Environment Under Windows
MS-DOS Programs on Windows
Configuring the MS-DOS Environment
MS-DOS and Networking
Printing from MS-DOS
Configuring Serial Communications with MS-DOS
Using Special-Purpose Devices for MS-DOS
Managing MS-DOS Programs
13 Command-Line Utilities
Windows Command-Line Programs
The Essential Command Line
GUI Shortcuts
General-Purpose Shell Programs
File-Management Tools
Management Power Tools
Networking Utilities
Getting More Utilities
III: Introduction to Windows PowerShell
14 Windows PowerShell
Introduction to Windows PowerShell
Obtaining Windows PowerShell
The PowerShell Environment
The PowerShell Command Prompt
Cmdlets and Objects and Scripts, Oh My!
Getting Help
Prompting to Complete Commands
Aliases
Navigating Directories and Other Locations
PowerShell Security
PowerShell Profiles
15 PowerShell Programming
The Windows PowerShell Programming Language
Windows PowerShell Syntax
Comments
Variables and Types
Expressions
Flow of Control
Exception Handling
Defining Functions
Using the .NET API
16 Using PowerShell
Real-World PowerShell
Command-Line Techniques
Formatting Cmdlet Output
Working with Files and Folders
Creating Useful Scripts
Using Hash Tables
The PowerShell Integrated Scripting Environment
Remote and Background PowerShell
Where to Go from Here
IV: Appendices
A: VBScript Reference
VBScript 5.6 Language Features
Syntax
General Structure of a VBScript Program
Data Types and Variables
Expressions and Operators
Program Statements
Functions
Predefined Special Values
VBA Features Omitted from VBScript
B: CMD and Batch File Language Reference
Batch File Argument and for Variable Replacement
Environment Variable Expansion
Predefined Environment Variables
Command Formatting
Built-in Commands
C: Command Line Program Reference
Administrative Tools
Built-in and Batch File Commands
DOS Commands
File-Management Commands
Handy Programs
Networking Tools
Software Development Aids
TCP/IP Utilities
Windows GUI Programs
D: Index of Patterns and Sample Scripts
Index of Patterns
Index of Sample Scripts and Batch Files
Index
A
B
C
D
E
G
H
I
J-K
L
M
N
O
P
Q-R
S
T
U
V
W
X-Y-Z
E: Automation Object Reference
Collection and Dictionary Objects
Script Management and Utility Objects
File Access Objects
XML/HTML Processing Objects
Program Environment Objects
Network and Printer Objects
Messaging Objects
Windows Management Interface (WMI) Objects
Active Directory Scripting Interface Objects
F: WSF and WSC File Format Reference
XML Conformance
Structure of a WSF
Structure of a WSC File
Tag Syntax
G: Creating Your Own Scriptable Objects
Why Create Your Own Objects?
Programming Language Options
Creating Objects with Windows Script Component Files
WSC File Format Reference
Creating a WSC
Creating a Practical Object
ptg6519196
Windows 7 and Vista Guide to Scripting, Automation, and Command Line Tools Brian Knittel 800 East 96th Street Indianapolis, IN 46240 USA
Windows 7 and Vista: Guide to Scripting, Automation, and Command Line Tools Copyright © 2011 by Pearson Education, Inc. All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmit- ted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written per- mission 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 pub- lisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein. ISBN-13: 978-0-7897-3728-1 ISBN-10: 0-7897-3728-0 Library of Congress Cataloging-in-Publication data is on file. Printed in the United States of America First Printing: December 2010 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appro- priately capitalized. Que Publishing 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. Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no war- ranty or fitness is implied.The information provided is on an “as is” basis.The author and the pub- lisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book. Bulk Sales Que Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contact U.S. Corporate and Government Sales 1-800-382-3419 corpsales@pearsontechgroup.com For sales outside of the United States, please contact International Sales international@pearson.com Associate Publisher Greg Wiegand Acquisitions Editor Rick Kughen Development Editor Todd Brakke Managing Editor Sandra Schroeder Project Editor Mandie Frank Copy Editor Megan Wade Indexer Tim Wright Proofreader Jovana Shirley Technical Editor Ron Barrett Publishing Coordinator Cindy Teeters Designer Anne Jones Compositor Studio Galou, LLC ptg6519196
Contents at a Glance Introduction 1 I Scripting with Windows Script Host 1 Windows Script Host 9 2 VBScript Tutorial 49 3 Scripting and Objects 93 4 File and Registry Access 123 5 Network and Printer Objects 207 6 Messaging and Faxing Objects 231 7 Windows Management Interface 279 8 Active Directory Scripting Interface 319 9 Deploying Scripts for Computer and Network Management 375 II The Command Line Environment 10 The CMD Command-Line Shell 433 11 Batch Files for Fun and Profit 491 12 The MS-DOS Environment Under Windows 521 13 Command-Line Utilities 543 III Introduction to Windows PowerShell 14 Windows PowerShell 593 15 PowerShell Programming 621 16 Using PowerShell 683 IV Appendices A VBScript Reference 713 B CMD and Batch File Language Reference 725 C Command Line Program Reference 735 D Index of Patterns and Sample Programs 747 Index 753 Online Content: E Automation Object Reference F WSF and WSC File Format Reference G Creating Your Own Scriptable Objects ptg6519196
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? 13 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 and VBE: 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 iv Wow! eBook ptg6519196
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 67 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 84 Advanced VBScript Topics 85 Error Handling 86 Procedures: Functions and Subroutines 87 v Wow! eBook ptg6519196
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 100 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 WScript 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 vi Wow! eBook ptg6519196
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 214 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 vii Wow! eBook ptg6519196
分享到:
收藏