Windows Powershell 2.0 Best Practices Ebook
Total Page:16
File Type:pdf, Size:1020Kb
● ● ● ● ● ● ● ● ● ● ● How to access your CD files The print edition of this book includes a CD. To access the CD files, go to http://aka.ms/626461/files, and look for the Downloads tab. Note: Use a desktop web browser, as files may not be accessible from all ereader devices. Questions? Please contact: [email protected] Microsoft Press PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2010 by Ed Wilson All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher. Library of Congress Control Number: 2009938599 Printed and bound in the United States of America. 1 2 3 4 5 6 7 8 9 WCT 4 3 2 1 0 9 Distributed in Canada by H.B. Fenn and Company Ltd. A CIP catalogue record for this book is available from the British Library. Microsoft Press books are available through booksellers and distributors worldwide. For further infor mation about international editions, contact your local Microsoft Corporation office or contact Microsoft Press International directly at fax (425) 936-7329. Visit our Web site at www.microsoft.com/mspress. Send comments to [email protected]. Microsoft, Microsoft Press, Access, Active Accessibility, Active Directory, ActiveX, Authenticode, Excel, Expression, Forefront, Groove, Hyper-V, IntelliSense, Internet Explorer, MSDN, OneNote, Outlook, ReadyBoost, Segoe, SharePoint, Silverlight, SQL Server, Visual Basic, Visual SourceSafe, Visual Studio, Win32, Windows, Windows Media, Windows NT, Windows PowerShell, Windows Server, and Windows Vista are either registered trademarks or trademarks of the Microsoft group of companies. Other product and company names mentioned herein may be the trademarks of their respective owners. The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred. This book expresses the author’s views and opinions. The information contained in this book is provided without any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. Acquisitions Editor: Martin DelRe Developmental Editor: Karen Szall Project Editor: Melissa von Tschudi-Sutton Editorial Production: Custom Editorial Productions, Inc. Technical Reviewer: Randall Galloway; Technical Review services provided by Content Master, a member of CM Group, Ltd. Cover: Tom Draper Design Body Part No. X16-38608 This book is dedicated to Teresa Wilson, my best friend, life companion, inspiration, and wife. You made me so happy when you said yes. Contents at a Glance Acknowledgments xvii Introduction xix About the Companion Media xxv PART I INTRODUCTION CHApteR 1 Assessing the Scripting Environment 3 CHApteR 2 Survey of Windows PowerShell Capabilities 27 CHApteR 3 Survey of Active Directory Capabilities 65 CHApteR 4 User Management 99 PART II PLANNING CHApteR 5 Identifying Scripting Opportunities 133 CHApteR 6 Configuring the Script Environment 167 CHApteR 7 Avoiding Scripting Pitfalls 207 CHApteR 8 Tracking Scripting Opportunities 253 PART III DESIGNING CHApteR 9 Designing Functions 293 CHApteR 10 Designing Help for Scripts 331 CHApteR 11 Planning for Modules 373 CHApteR 12 Handling Input and Output 407 CHApteR 13 Handling Errors 461 PART IV TESTING AND DEplOYING CHApteR 14 Testing Scripts 499 CHApteR 15 Running Scripts 543 PART V OpTIMIZING CHApteR 16 Logging Results 577 CHApteR 17 Troubleshooting Scripts 601 Appendix A 635 Appendix B 647 Appendix C 651 Appendix D 655 Appendix E 693 Appendix F 697 Index 699 Contents Acknowledgments xvii Introduction xix About the Companion Media xxv PART I INTRODUCTION Chapter 1 Assessing the Scripting Environment 3 Why Use Windows PowerShell 2.0?. .3 Comparison with Windows PowerShell 1.0. 4 Backward Compatibility 4 Using the Version Tag 4 New Features of Windows PowerShell 2.0. .8 New cmdlets 9 Modified cmdlets 12 Architectural Changes 14 Comparing Windows PowerShell 2.0 to VBScript. 16 The Learning Curve 17 Component Object Model (COM) Support 18 Comparing Windows PowerShell 2.0 to the Command Shell 21 Deployment Requirements for Windows PowerShell 2.0 . 22 .NET Framework 22 Service Dependencies 23 Deploying Windows PowerShell 2.0. 23 Where to Deploy Windows PowerShell 2.0 24 How Do You Anticipate Using Windows PowerShell 2.0? 24 Additional Resources. 25 What do you think of this book? We want to hear from you! Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you. To participate in a brief online survey, please visit: microsoft.com/learning/booksurvey vii Chapter 2 Survey of Windows PowerShell Capabilities 27 Using the Interactive Command Line. 27 The Easiest cmdlets 30 The Most Important cmdlets 31 Grouping and Sorting Output 36 Saving Output to Files 38 Working with WMI. 38 Obtaining Information from Classes 41 Finding WMI Classes 42 Setting Properties 43 Calling Methods 45 Working with Instances 46 Working with Events 47 Working Remotely. 48 Using –computer 48 Creating a Remote Interactive Session 63 Running a Remote Command 63 Additional Resources. 64 Chapter 3 Survey of Active Directory Capabilities 65 Creating Users, Groups, and Organizational Units. 66 Creating Objects. 67 Creating a User Account 69 Creating a Group 69 Creating a Computer Account 69 Deriving the Create Object Pattern. 75 Modifying the Variables 75 Constant vs. Read-Only Variables 76 Creating a Utility Script 76 Using CSV Files to Create Multiple Objects 77 Testing a Script 81 Configuring the Connection to the Database. 81 Using ADO.NET 82 Using ADO COM Objects 86 viii Contents Modifying Properties. 88 Using Excel to Update Attributes 89 Making the Connection 91 Reading the Data 92 Making the Changes 93 Closing the Connection 94 Additional Resources. 97 Chapter 4 User Management 99 Examining the Active Directory Schema. 99 Querying Active Directory. .110 Using ADO 112 Using Directory Searcher 117 Using [ADSISearcher] 120 Performing Account Management. .123 Locating Disabled User Accounts 123 Moving Objects 125 Searching for Missing Values in Active Directory 127 Additional Resources. .130 PART II PLANNING Chapter 5 Identifying Scripting Opportunities 133 Automating Routine Tasks. .133 Automation Interface 133 Using RegRead to Read the Registry 134 Structured Requirements. 138 Security Requirements 138 .NET Framework Version Requirements 154 Operating System Requirements 158 Application Requirements 162 Snap-in Requirements 164 Additional Resources. .165 Contents ix Chapter 6 Configuring the Script Environment 167 Configuring a Profile . .167 Creating Aliases 168 Creating Functions 172 Passing Multiple Parameters 176 Creating Variables 181 Creating PSDrives 188 Enabling Scripting 194 Creating a Profile. .196 Choosing the Correct Profile 197 Creating Other Profiles 199 Accessing Functions in Other Scripts. 202 Creating a Function Library 203 Using an Include File 204 Additional Resources. .206 Chapter 7 Avoiding Scripting Pitfalls 207 Lack of cmdlet Support. .207 Complicated Constructors. .209 Version Compatibility Issues. .211 Trapping the Operating System Version 217 Lack of WMI Support. .220 Working with Objects and Namespaces. .220 Listing WMI Providers . 224 Working with WMI Classes. 225 Changing Settings 229 Modifying Values Through the Registry 232 Lack of .NET Framework Support. .238 Use of Static Methods and Properties 238 Version Dependencies 241 Lack of COM Support 241 Lack of External Application Support 248 Additional Resources. .252 x Contents Chapter 8 Tracking Scripting Opportunities 253 Evaluating the Need for the Script. 253 Reading a Text File 254 Export Command History 259 Fan-out Commands 261 Query Active Directory 265 Just Use the Command Line 272 Calculating the Benefit from the Script. 276 Repeatability 277 Documentability 280 Adaptability 281 Script Collaboration. 285 Windows SharePoint Services 286 Microsoft Office Groove 288 Live Mesh 288 Additional Resources. .290 PART III DESIGNING Chapter 9 Designing Functions 293 Understanding Functions . .293 Using Functions to Provide Ease of Code Reuse. 301 Using Two Input Parameters 304 Using a Type Constraint 309 Using More than Two Input Parameters. .311 Using Functions to Encapsulate Business Logic. .313 Using Functions to Provide Ease of Modification. .315 Understanding Filters. .324 Additional Resources. .328 Contents xi Chapter 10 Designing Help for Scripts 331 Adding Help Documentation to a Script with Single-Line Comments. .331 Using the Here-String for Multiple-Line Comments. .335 Constructing a Here-String 336 An Example: Adding Comments to a Registry Script 337 Retrieving Comments by Using Here-Strings 345 Using Multiple-Line Comment Tags in Windows PowerShell 2.0. 355 Creating Multiple-Line Comments with Comment Tags 355 Creating Single-Line Comments with Comment Tags 356 The 13 Rules for Writing Effective Comments . .357 Update Documentation When a Script Is Updated 357 Add Comments During the Development Process 358 Write for an International Audience 359 Consistent Header Information 360 Document Prerequisites 361 Document Deficiencies 362 Avoid Useless Information 364 Document the Reason