● ● ● ● ● ● ● ● ● ● ● 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: 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 inform ation about international editions, contact your local Microsoft Corporation offce or contact Microsoft Press International directly at fax (425) 936-7329. Visit our Web site at www.microsoft.com/mspress. Send comments to 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 Confguring 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 Modifed 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 Confguring the Connection to the Database . . . . . . . . . . . . . . . . . . . . . . . . 81 Using ADO.NET 82 Using ADO COM Objects 86 viii Contents