Don Jones VBScript , WMI ADSI , and Using VBScript, WMI, and ADSI to Automate Windows Administration ® U N L E A S H E D 800 East 96th Street,Indianapolis,Indiana 46240 USA VBScript,WMI,and ADSI Unleashed Editor-in-Chief Copyright © 2007 by Sams Publishing Karen Gettman All rights reserved. No part of this book shall be reproduced,stored in a retrieval system,or trans- mitted by any means,electronic,mechanical,photocopying,recording,or otherwise,without written Acquisitions Editor permission from the publisher. No patent liability is assumed with respect to the use of the infor- Joan Murray mation 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. Nor is any liabil- Managing Editor ity assumed for damages resulting from the use of the information contained herein. Gina Kanouse This material may be distributed only subject to the terms and conditions set forth in the Open Senior Project Editor Publication License,v1.0 or later (the latest version is presently available at http://www.opencon- Kristy Hart tent.org/openpub/). ISBN-13: 978-0-321-50171-4 Copy Editor ISBN-10: 0-321-50171-3 Karen Annett Library of Congress Cataloging-in-Publication Data: Jones,Don,1971- Indexer VBScript,WMI and ADSI unleashed : using VBSscript,WMI,and ADSI to automate Windows Brad Herriman administration / Don Jones. — 1st ed. Proofreader p. cm. Williams Woods ISBN 0-321-50171-3 (pbk. : alk. paper) 1. VBScript (Computer program language) 2. Microsoft Publishing,LLC Windows (Computer file) 3. Directory services (Computer network technology) I. Title. QA76.73.V27J67 2007 Cover Designer 005.13’3—dc22 Gary Adair 2007008741 Composition Printed in the United States on America Bronkella Publishing First Printing May 2007 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appro- priately capitalized. Sams 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 warranty or fitness is implied. The information provided is on an “as is” basis. The author and the publisher 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 Sams 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 [email protected] For sales outside of the U.S.,please contact International Sales [email protected] Contents at a Glance Introduction...................................................................................1 Part I Introduction to Windows Administrative Scripting 1 Scripting Concepts and Terminology.................................................13 2 Running Scripts.............................................................................21 3 The Components of a Script............................................................39 4 Designing a Script..........................................................................55 Part II VBScript Tutorial 5 Functions, Objects, Variables, and More.............................................75 6 Input and Output.........................................................................101 7 Manipulating Numbers .................................................................115 8 Manipulating Strings.....................................................................129 9 Manipulating Other Types of Data..................................................145 10 Controlling the Flow of Execution..................................................155 11 Built-In Scripting Objects...............................................................173 12 Working with the File System.........................................................193 13 Putting It All Together: Creating Your First Script from Scratch.............219 Part III Windows Management Instrumentation and Active Directory Services Interface 14 Working with ADSI Providers.........................................................247 15 Manipulating Domains..................................................................257 16 Manipulating Users and Groups......................................................271 17 Understanding WMI.....................................................................283 18 Querying Basic WMI Information...................................................299 19 Querying Complex WMI Information..............................................317 20 Putting It All Together: Your First WMI/ADSI Script............................335 21 Testing and Debugging WMI and ADSI Queries.................................357 Part IV Advanced Scripting Techniques 22 Modular Script Programming..........................................................373 23 Scripts Packaging and Protection.....................................................389 24 Scripting Security.........................................................................399 25 Introduction to HTML Applications.................................................409 26 Debugging Tips, Tools, and Techniques............................................421 Part V Ready-to-Run Examples 27 Logon and Logoff Scripts...............................................................439 28 Windows and Domain Administration Scripts...................................455 29 Network Administration Scripts......................................................481 30 WMI and ADSI Scripts...................................................................497 Appendix....................................................................................509 Index.........................................................................................523 Table of Contents Introduction 1 Who Should Read This Book?............................................................2 How to Use This Book......................................................................2 Part I: Introduction to Windows Administrative Scripting...............3 Part II: VBScript Tutorial............................................................3 Part III: Windows Management Instrumentation and Active Directory Services Interface......................................................5 Part IV: Advanced Scripting Techniques.......................................6 Part V: Ready-to-Run Examples...................................................7 Appendix................................................................................8 Preparing to Use This Book................................................................8 Typographical Elements....................................................................8 Sample Scripts.........................................................................9 Sample Scripts—Explained.........................................................9 Part I Introduction to Windows Administrative Scripting 1 Scripting Concepts and Terminology 13 Overview of Scripting.....................................................................13 Script Hosts...................................................................................14 ActiveX Scripting Languages............................................................16 The Component Object Model (COM)...............................................18 Critical Scripting Security Issues.......................................................18 VBScript Versus Windows PowerShell................................................19 Summary......................................................................................20 2 Running Scripts 21 Windows Script Host......................................................................21 Command-Line Scripts....................................................................23 Notepad and Script Editors..............................................................25 Bare Bones: Notepad...............................................................26 A Step Up: Programmer’s File Editor..........................................26 Script-Specific: VBScript Editors................................................27 All-Inclusive: VBScript IDEs......................................................29 vi VBScript, WMI, and ADSI Unleashed Writing Your First Script..................................................................33 Running Your First Script.................................................................35 Debugging Your First Script..............................................................35 Summary......................................................................................37 3 The Components of a Script 39 A Typical VBScript..........................................................................39 Functions.....................................................................................42 Subroutines...................................................................................43 Main Script...................................................................................44 Using Custom Functions and Subroutines..................................45 Using Intrinsic Functions and Statements...................................47 Making Decisions in a Script....................................................48 Comments and Documentation.......................................................48 Summary......................................................................................53 4 Designing a Script 55 Creating a Task List........................................................................56 Selecting the Appropriate Tools........................................................58 Displaying a Message..............................................................59 Mapping a Drive....................................................................60 Checking Group Membership...................................................61 Mapping a Printer..................................................................61 Getting the Local IP Address....................................................61 Getting the Third Octet from the IP Address...............................63 All Tasks Accounted For...........................................................63 Creating Modules to Perform Tasks...................................................63 Validating User Input......................................................................69 Planning for Errors.........................................................................70 Creating Script Libraries..................................................................72 Summary......................................................................................72 Part II VBScript Tutorial 5 Functions,Objects,Variables,and More 75 Variables Defined...........................................................................76 Declaring Variables.................................................................79 Understanding Data Types.......................................................82 Assigning Data to Variables......................................................82 Data Coercion........................................................................83 Contents vii Functions Defined..........................................................................85 Input Parameters....................................................................85 Output Values........................................................................87 Intrinsic Versus Custom Functions............................................88 Statements and Subroutines Defined.................................................92 Functions, Without the Output................................................92 Objects Defined.............................................................................94 Properties..............................................................................95 Methods...............................................................................95 Collections............................................................................96 A Sample Object.....................................................................97 Scripting with Objects.............................................................98 Summary......................................................................................99 6 Input and Output 101 Displaying Messages.....................................................................101 TheMsgBoxStatement and Function........................................102 More Complex Messages........................................................105 MsgBox Best Practices...........................................................106 Go Generic with WScript.Echo...............................................107 Asking for Input...........................................................................108 Graphical Input....................................................................108 Command-Line Input............................................................109 Command-Line Parameters as Input................................................110 Running Command-Line Scripts.............................................111 Parsing Parameters................................................................111 Summary....................................................................................114 7 Manipulating Numbers 115 Numbers in VBScript.....................................................................115 Basic Arithmetic...........................................................................116 Advanced Arithmetic....................................................................118 Boolean Math..............................................................................121 Converting Numeric Data Types.....................................................126 Converting Other Data Types to Numeric Data..................................127 Summary....................................................................................127 8 Manipulating Strings 129 Strings in VBScript........................................................................129 Working with Substrings................................................................131 viii VBScript, WMI, and ADSI Unleashed Concatenating Strings...................................................................135 Changing Strings..........................................................................139 Formatting Strings........................................................................141 Converting Other Data Types to String Data.....................................142 Summary....................................................................................143 9 Manipulating Other Types of Data 145 Working with Dates and Times.......................................................145 Dates and Times in VBScript...................................................145 Getting the Date or Time.......................................................146 Converting Date and Time Data..............................................147 Working with Past and Future Dates........................................147 Working with Arrays.....................................................................148 Arrays in VBScript.................................................................149 Creating and Manipulating Arrays...........................................149 Working with Array Data.......................................................151 Working with Bytes......................................................................152 Bytes in VBScript..................................................................152 Converting Byte Data............................................................152 Summary....................................................................................153 10 Controlling the Flow of Execution 155 Conditional Execution..................................................................155 If/Then...............................................................................156 Select/Case........................................................................161 Loops.........................................................................................162 Do While/LoopandDo/Loop While..........................................162 Do Until/LoopandDo/Loop Until..........................................163 For/Next.............................................................................164 For Each/Next.....................................................................165 Exiting Loops.......................................................................166 Putting It All Together...................................................................167 Summary....................................................................................170 11 Built-In Scripting Objects 173 TheWScriptObject......................................................................173 TheNetworkObject......................................................................174 Methods and Properties.........................................................175 Practical Application.............................................................178 TheADSystemInfoObject...............................................................179 Contents ix TheShellObject .........................................................................180 Methods and Properties.........................................................180 TheShortcutObject.....................................................................187 Methods and Properties.........................................................187 Practical Application.............................................................188 Objects in Detail..........................................................................189 Summary....................................................................................191 12 Working with the File System 193 TheFileSystemObjectLibrary........................................................193 Working with Drives.....................................................................194 Working with DriveObjects...................................................195 Working with Folders....................................................................197 Working with FolderObjects.................................................198 Folder Attributes...................................................................200 Properties That Are Objects....................................................202 Working with Files........................................................................203 Working with FileObjects ....................................................204 Reading and Writing Text Files.......................................................206 Other FSO Methods and Properties..................................................209 Creating a Log File Scanner............................................................211 Summary....................................................................................218 13 Putting It All Together: Creating Your First Script from Scratch 219 Designing the Script......................................................................219 Gathering Facts....................................................................220 Defining Tasks......................................................................221 Outlining the Script..............................................................222 Writing Functions and Subroutines.................................................223 Identifying Candidate Modules...............................................223 Writing the Filename Formatting Function...............................223 Writing the Main Script.................................................................228 Identifying Potential Errors....................................................231 Testing the Script..........................................................................234 Analyzing the Results............................................................236 Adding Debug Code..............................................................236 Modifying the Script.............................................................240 Completing the Script...........................................................241 Summary....................................................................................244
Description: