Product Cover Image

Windows 7 and Vista Guide to Scripting, Automation, and Command Line Tools

By Brian Knittel

Published by Que

Published Date: Dec 1, 2010

More Product Info

Description

The addition of the mouse and the Graphical User Interface made the computer accessible to many more people than would have been possible otherwise. Still, "pointing and clicking" can be tedious when performing routine or repetitive tasks. Just as people learn to use menu shortcuts (the Alt key) to save time, savvy users and administrators learn to write batch files and scripts to avoid having to type the same commands over and over. Scripts save time, increase accuracy, and serve as documentation to boot. Windows 7 and Windows Vista come with scripting, batch file, and command line tools that can make a power user or administrator's life easier - if she or he knows what they are and how to use them. However, a general lack of information and awareness prevent their widespread use. Most current Windows users have no idea how powerful and effective these tools can be. The new scripting languages are a mystery to most Windows users. And, in Windows 7, most command line tools aren't even discussed in the Windows Help system. Microsoft has released a completely new scripting and command line environment called the Windows PowerShell, but few users are aware of its existence, let alone its power and potential.

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

    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

        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

    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

6 Messaging and Faxing Objects 231

    Sending Email from Scripts with CDO 231

    The CDO Object Model 232

        The CDO.Message Object 235

        Working with Fields 242

        Fields for the CDO.Message Object 244

        The CDO BodyParts Collection 246

        The CDO BodyPart Object 247

        The ADO Stream Object 250

        The CDO.Configuration Object 250

    Sending a Message with CDO 256

        Constructing the Message 257

        Adding Attachments 261

        Including Images with an HTML Message 262

        Specifying the Recipients and Subject 263

        Specifying the Delivery Server 263

        Sending the Message 265

        Putting It All Together 265

    Faxing from Scripts 271

        Sending a Fax with a Script 274

        Getting More Information About Faxing 277

7 Windows Management Instrumentation 279

    Introduction to Windows Management Instrumentation 279

        WMI Functions 280

        Namespaces 281

        Managing Windows Remotely 283

    Making WMI Connections 287

        WMI Object Hierarchy 288

        Connecting with the WbemScripting.SWbemLocator Object 291

        Connecting with a Moniker 292

        Connecting to the Local Computer 294

        Security and Authentication 294

        Specifying Security Options 299

    WMI Collections and Queries 301

        SWbemServices 302

        WQL Queries 303

        SWbemObject 306

        SWbemMethodSet and SWbemPropertySet 307

    Scriptomatic 310

    WMI Examples 312

        Collecting System Information 312

        Managing Printers 313

        Monitoring Windows Service Packs and Hotfixes 313

        Managing Services and Tasks 315

    For More Information 317

8 Active Directory Scripting Interface 319

    Managing the User Directory 319

        Uses of the Active Directory Scripting Interface 320

        Limitations of ADSI with Windows Script Host 321

    ADSI Concepts 322

        Multiple Inheritance 324

        Creating ADSI Objects 325

        Directory Security 328

        Determining the Difference Between Containers and Leaves 330

    ADSI Objects for the WinNT: Provider 332

        IADs 333

        IADsCollection and IADsContainer 336

        Working with ADSI Collections 339

        IADsComputer and IADsComputerOperations 340

        IADsDomain 342

        IADsFileService and IADsFileServiceOperations 345

        IADsFileShare 347

        IADsGroup 349

        IADsMembers 350

        IADsNamespaces 351

        IADsPrintJob and IADsPrintJobOperations 351

        IADsPrintQueue and IADsPrintQueueOperations 354

        IADsService and IADsServiceOperations 357

        IADsSession 361

        IADsUser 362

    IIS and Exchange 364

    Managing Active Directory 364

        X.500 and LDAP Terminology 364

    Active Directory Objects 368

        RootDSE 368

        IADsO and IADsOU 369

    Developing ADSI Scripts 370

    EzAD Scriptomatic 372

    For More Information 373

9 Deploying Scripts for Computer and Network Management 375

    Using Scripts in the Real World 375

        Designing Scripts for Other Users 376

    Using WSF Files 377

        WSF File Format Reference 379

        Providing Online Help with WSF Files 384

        Processing Command-Line Arguments 386

        Enclosing More Than One Script 390

        Putting It All Together 390

    Deploying Scripts on a Network 394

    Creating Simple Installation Programs with IExpress 395

        Creating IExpress Install Scripts or Batch Files 398

        Dealing with User Account Control 400

        Providing an Uninstall Option 402

    Writing Scripts to Manage Other Computers 403

        Remote Scripting 405

        Replicating Scripts to Multiple Computers 406

    Scripting Security Issues 408

        Script Signing 409

        The Script Encoder 415

    Setting Up Logon Scripts 416

        User Profile Logon Scripts 416

        Scripts for Logon, Logoff, and Other Events on Windows 7 and Vista 418

        Group Policy Logon, Logoff, Startup, and Shutdown Scripts 418

    Scheduling Scripts to Run Automatically 421

        Writing Unattended Scripts 421

        Sending Messages to the Event Log 423

        Scheduling Scripts with the Task Scheduler 428

II The Command Line Environment

10 The CMD Command-Line 433

    The Command Prompt 433

        CMD Versus COMMAND 434

    Running CMD 435

        Opening a Command Prompt Window with Administrator

        Privileges 436

        CMD Options 437

        Disabling Command Extensions 439

    Command-Line Processing 439

        Stopping Runaway Programs 440

        Console Program Input and Output 441

        Using the Console Window 442

        I/O Redirection and Pipes 443

        Copy and Paste in Command Prompt Windows 447

        Command Editing and the History List 448

        Name Completion 450

        Enabling Directory Name Completion 451

        Multiple Commands on One Line 452

        Grouping Commands with Parentheses 453

        Arguments, Commas, and Quotes 454

        Escaping Special Characters 454

    Configuring the CMD Program 455

        AutoRun 455

        Environment Variable Substitution 456

        The Search Path 456

        Predefined and Virtual Environment Variables 459

        Setting Default Environment Variables 461

    Built-in Commands 462

        Extended Commands 475

        Listing Files with the Dir Command 476

        Setting Variables with the Set Command 480

        Conditional Processing with the if Command 482

        Scanning for Files with the for Command 483

    Getting More Information 488

    11 Batch Files for Fun and Profit 491

    Why Batch Files? 491

    Creating and Using Batch Files 492

    Batch File Programming 494

    Displaying Information in Batch Files 495

    Argument Substitution 496

    Argument Editing 498

    Conditional Processing with If 499

        The Basic If Command 499

        Checking for Files and Folders 500

        Checking the Success of a Program 500

        Performing Several Commands After If 501

        Extended Testing 503

    Processing Multiple Arguments 503

    Working with Environment Variables 506

        Environment Variable Editing 507

    Processing Multiple Items with the for Command 508

        Using Multiple Commands in a for Loop 510

        Delayed Expansion 511

    Using Batch File Subroutines 513

    Prompting for Input 514

    Useful Batch File Techniques 515

        Processing Command-Line Options 515

        Managing Network Mappings 518

        Checking for Correct Arguments 519

        Keeping Log Files 519

12 The MS-DOS Environment Under Windows 521

    MS-DOS Programs on Windows 521

        The Virtual DOS Machine 522

        MS-DOS and COMMAND.COM 524

    Configuring the MS-DOS Environment 525

        Window and Memory Options 526

        CONFIG.NT 532

        AUTOEXEC.NT 535

        MS-DOS Environment Variables 536

    MS-DOS and Networking 536

    Printing from MS-DOS 537

        Print Redirection 538

        Print Screen 538

    Configuring Serial Communications with MS-DOS 539

    Using Special-Purpose Devices for MS-DOS 539

    Managing MS-DOS Programs 540

        When Things Go Awry 540

13 Command-Line Utilities 543

    Windows Command-Line Programs 543

    The Essential Command Line 544

    GUI Shortcuts 545

    General-Purpose Shell Programs 547

        findstr 547

        more 552

        tree 553

        xcopy 554

    File-Management Tools 557

        attrib 557

        cacls 559

    Management Power Tools 563

        driverquery 564

        runas 565

        tasklist 565

        taskkill 568

        sc 569

    Networking Utilities 571

        ipconfig 571

        net 574

        netstat 584

        nslookup 586

        ping 589

        tracert 591

    Getting More Utilities 592

III Introduction to Windows PowerShell

14 Windows PowerShell 593

    Introduction to Windows PowerShell 593

        An Object-Oriented Command Shell 593

        Based on the .NET Framework 596

        An Extensible Environment 597

    Obtaining Windows PowerShell 598

    The PowerShell Environment 600

    The PowerShell Command Prompt 601

        Command-Line Editing 602

        Copying and Pasting 603

        Pausing Output and Stopping a Runaway Program 604

        Command-Line Syntax 604

    Cmdlets and Objects and Scripts, Oh My! 607

    Getting Help 610

    Prompting to Complete Commands 612

    Aliases 612

        How to Get a Listing of Aliases 612

        How to Define a New Alias 613

    Navigating Directories and Other Locations 613

    PowerShell Security 615

        PowerShell Scripts and User Account Control 615

        Script Execution Policy 616

    PowerShell Profiles 617

15 PowerShell Programming 621

    The Windows PowerShell Programming Language 621

    Windows PowerShell Syntax 622

    Comments 622

    Variables and Types 623

        Literal Values 625

        Object Methods and Properties 626

        Object Constructors 627

        String Interpolation 628

        Special Characters 629

        Here-Strings 629

        Releasing Variables 630

        Predefined Variables 630

        Arrays 632

        Constants 637

    Expressions 638

        Comparisons with Arrays 640

        String Operators 643

        The & (Execute) Operator 646

        Operator Precedence 646

        Assignment Operators 647

        Statement Values 648

        Casts 649

        Passing by Reference 650

        Hash Tables 650

    Flow of Control 653

        if 653

        while 654

        do…while and do…until 654

        for 655

        foreach 656

        switch 657

        break 660

        continue 661

        Program Blocks 661

    Exception Handling 662

        trap 662

        try/catch/finally 663

        throw 664

    Defining Functions 664

        Function Parameters 665

        Function Scope 668

        The Dot-Source Operator 668

        Variable Scope 669

        Pipeline Functions and Filters 671

        Splatting 672

    Using the .NET API 673

        Calling Static Member Functions 673

        Working with Strings 674

        Working with Dates and Times 676

        Converting Values 680

        Mathematical Functions 680

16 Using PowerShell 683

    Real-World PowerShell 683

    Command-Line Techniques 685

        Generating Objects 685

        Filtering 686

        Taking Actions 689

    Formatting Cmdlet Output 690

        The -f Operator 690

    Working with Files and Folders 691

        Seeing Whether a File Exists 697

        Reading Text from Files 697

        Writing Text to Files 698

        Identifying Files by Size 698

    Creating Useful Scripts 699

        Comment Your Work! 700

        Command-Line Processing 700

        Writing Modules 701

        Exception Handling as an Exit Strategy 702

    Using Hash Tables 703

    The PowerShell Integrated Scripting Environment 704

        Starting the PowerShell ISE 705

        Configuring the ISE 706

        Creating and Editing Scripts 707

        Running Scripts in the ISE 708

        Setting Breakpoints and Single-Stepping 709

        Interactively Examining and Changing Variables 710

        Conditional Breakpoints 711

    Remote and Background PowerShell 712

    Where to Go from Here 712

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 Scripts 747

E Automation Object Reference 1 (Online)

F WSF and WSC File Format Reference 1 (Online)

G Creating Your Own Scriptable Objects 1 (Online)

 

Purchase Info

ISBN-10: 0-13-139004-X

ISBN-13: 978-0-13-139004-1

Format: Safari PTG

This publication is not currently for sale.