www.it-ebooks.info Tyson Kopczynski Windows ® PowerShell U N L E A S H E D 800 East 96th Street,Indianapolis,Indiana 46240 USA www.it-ebooks.info Windows® PowerShell 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 Senior Acquisitions system,or transmitted by any means,electronic,mechanical,photocopying,recording, or otherwise,without written permission from the publisher. No patent liability is Editor assumed with respect to the use of the information contained herein. Although every Neil Rowe precaution has been taken in the preparation of this book,the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for Development Editor damages resulting from the use of the information contained herein. Mark Renfrow International Standard Book Number: 0-672-32953-0 Managing Editor Library of Congress Cataloging-in-Publication Data Gina Kanouse Project Editor Kopczynski,Tyson. Microsoft PowerShell unleashed / Tyson Kopczynski. George E. Nedeff p. cm. Copy Editor ISBN 0-672-32953-0 1. Microsoft Windows (Computer file) 2. Operating systems (Computers) I. Title. Lisa M. Lord Senior Indexer QA76.76.O63K66 2007 005.4’46—dc22 Cheryl Lenser Proofreader 2007008894 Printed in the United States of America Water Crest First Printing: Publishing 10 09 08 07 4 3 2 1 Contributing Authors Trademarks Pete Handley,Mark All terms mentioned in this book that are known to be trademarks or service marks Weinhardt,and have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of JoshTolle this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Technical Editor Warning and Disclaimer Pawam Bhardwaj Every effort has been made to make this book as complete and as accurate as possi- ble,but no warranty or fitness is implied. The information provided is on an “as is” Publishing basis. The authors and the publisher shall have neither liability nor responsibility to any Coordinator person or entity with respect to any loss or damages arising from the information Cindy Teeters contained in this book. Book Designer Bulk Sales Gary Adair Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information,please contact Page Layout U.S. Corporate and Government Sales Jake McFarland 1-800-382-3419 Nonie Ratcliff [email protected] For sales outside of the U.S.,please contact International Sales [email protected] www.it-ebooks.info Contents at a Glance Introduction.................................................................................. 1 Part I Introduction to PowerShell 1 Introduction to Shells and PowerShell................................................ 7 2 PowerShell Basics.......................................................................... 19 3 PowerShell: A More In-Depth Look.................................................. 57 4 Code Signing................................................................................ 93 5 PowerShell Scripting Best Practices................................................. 107 Part II Translating Your Existing Knowledge into PowerShell 6 PowerShell and the File System.......................................................125 7 PowerShell and the Registry...........................................................157 8 PowerShell and WMI.................................................................... 183 9 PowerShell and Active Directory.................................................... 205 Part III Using PowerShell to Meet Your Automation Needs 10 Using PowerShell in the Real-World................................................ 235 11 Using PowerShell to Manage Exchange........................................... 261 Index........................................................................................ 295 www.it-ebooks.info Table of Contents Introduction 1 Part I Introduction to PowerShell 1 Introduction to Shells and PowerShell 7 What Is a Shell?...............................................................................7 Basic Shell Use.........................................................................8 Basic Shell Scripts...................................................................10 A Shell History..............................................................................14 Enter PowerShell............................................................................16 Summary......................................................................................17 2 PowerShell Basics 19 Introduction.................................................................................19 Getting Started..............................................................................19 Accessing PowerShell......................................................................21 Understanding the Command-Line Interface (CLI)..............................22 Navigating the CLI.................................................................24 PowerShell Command Types....................................................26 Calling PowerShell from Other Shells........................................30 Understanding cmdlets...................................................................33 Common Parameters..............................................................34 Useful cmdlets...............................................................................35 Get-Help...............................................................................35 Get-Command.......................................................................38 Expressions...................................................................................40 Understanding Variables .................................................................41 Built-in Variables....................................................................42 Understanding Aliases.....................................................................45 Discovering Alias cmdlets........................................................47 Creating Persistent Aliases........................................................48 Escape Sequences...........................................................................49 Understanding Scopes.....................................................................50 Global..................................................................................50 Local....................................................................................51 Script...................................................................................51 www.it-ebooks.info Table of Contents v Private..................................................................................52 Your First Script.............................................................................53 Summary......................................................................................56 3 PowerShell: A More In-Depth Look 57 Introduction.................................................................................57 Object Based.................................................................................57 Understanding the Pipeline......................................................58 .NET Framework Tips..............................................................60 Understanding Providers.................................................................72 Accessing Drives and Data.......................................................74 Mounting a Drive...................................................................77 Understanding Errors......................................................................78 Error Handling..............................................................................80 Method One: cmdlet Preferences...............................................80 Method Two: Trapping Errors...................................................81 Method Three: The Throw Keyword..........................................83 PowerShell Profiles.........................................................................84 The All Users Profile...............................................................84 The All Users Host-Specific Profile.............................................84 The Current User’s Profile........................................................85 The Current User’s Host-Specific Profile......................................85 Understanding Security...................................................................85 Execution Policies..................................................................86 Setting the Execution Policy.....................................................88 Additional Security Measures....................................................91 The PowerShell Language................................................................91 Summary......................................................................................91 4 Code Signing 93 Introduction.................................................................................93 What Is Code Signing?....................................................................94 Obtaining a Code-Signing Certificate.................................................95 Method One: Self-Signed Certificate..........................................96 Method Two: CA Signed Certificate...........................................97 The PVK Digital Certificate Files Importer...................................99 Signing PowerShell Scripts.............................................................100 Verifying Digital Signatures............................................................101 Signed Code Distribution...............................................................102 Enterprise Code Distribution..................................................105 Public Code Distribution........................................................105 Summary....................................................................................106 www.it-ebooks.info vi Microsoft PowerShell Unleashed 5 PowerShell Scripting Best Practices 107 Introduction................................................................................107 Script Development......................................................................107 Treat Scripting Projects as Actual Projects..................................108 Use a Development Life Cycle Model.......................................108 Design and Prototype Your Scripts by Using Pseudocode.............109 Gather Script Requirements Effectively.....................................109 Don’t Develop Scripts in a Production Environment...................109 Test, Test, Test......................................................................110 Keep Your Scripts Professional.................................................110 Script Design...............................................................................110 Put Configuration Information at the Beginning of Script...........110 Use Comments.....................................................................111 Avoid Hard-Coding Configuration Information.........................111 When Necessary, Use Variables in One Place.............................112 Provide Instructions..............................................................112 Perform Validity Checking on Required Parameters....................113 Make Scripts and Functions Reusable.......................................114 Use Descriptive Names Rather Than Aliases...............................114 Provide Status Information for Script Users...............................115 Use the WhatIf and Confirm Parameters...................................115 Script Security..............................................................................117 Digitally Sign PowerShell Scripts and Configuration Files............117 Never Set Execution Policies to Unrestricted..............................118 Try to Run Scripts with the Minimum Required Rights................118 Standards for Scripting..................................................................118 This Book’s Scripting Standards...............................................119 Summary....................................................................................120 Part II Translating Your Existing Knowledge into PowerShell 6 PowerShell and the File System 125 Introduction................................................................................125 File System Management in WSH and PowerShell..............................125 Working with Drives.............................................................126 Working with Folders............................................................126 Working with Files................................................................127 Working with Permissions .............................................................128 Setting Permissions with SubInACL.........................................128 Setting Permissions in PowerShell............................................131 From VBScript to PowerShell..........................................................136 TheProvisionWebFolders.wsfScript.......................................136 www.it-ebooks.info Table of Contents vii TheProvisionWebFolders.ps1Script.......................................146 Summary....................................................................................155 7 PowerShell and the Registry 157 Introduction................................................................................157 Registry Management in WSH and PowerShell..................................157 From VBScript to PowerShell..........................................................161 TheLibraryRegistry.vbsScript.............................................162 TheLibraryRegistry.ps1Script.............................................169 Summary....................................................................................181 8 PowerShell and WMI 183 Introduction................................................................................183 Comparing WMI Usage Between WSH and PowerShell.......................183 Using WMI in WSH..............................................................184 Using WMI in PowerShell......................................................185 Type Accelerators..................................................................187 From VBScript to PowerShell..........................................................191 TheMonitorMSVS.wsfScript...................................................191 TheMonitorMSVS.ps1Script...................................................197 Summary....................................................................................203 9 PowerShell and Active Directory 205 Introduction................................................................................205 Comparing ADSI Usage Between WSH and PowerShell.......................205 Using ADSI in WSH..............................................................207 Using ADSI with PowerShell...................................................208 Retrieving Object Information................................................209 Creating an Object................................................................210 From VBScript to PowerShell..........................................................212 TheIsGroupMember.wsfScript................................................212 TheIsGroupMember.ps1Script................................................222 Summary....................................................................................232 Part III Using PowerShell to Meet Your Automation Needs 10 Using PowerShell in the Real-World 235 ThePSShell.ps1Script.................................................................235 Component One: Shell Replacement........................................236 Component Two: PSShell.exe................................................239 Component Three: PSShell.ps1..............................................240 Putting It All Together...........................................................245 www.it-ebooks.info viii Microsoft PowerShell Unleashed TheChangeLocalAdminPassword.ps1Script.......................................246 Summary....................................................................................259 11 Using PowerShell to Manage Exchange 261 Introduction................................................................................261 The Exchange ManagementShell (EMS)............................................261 It’s Just a Snap-in..................................................................262 TheGetDatabaseSizeReport.ps1Script............................................266 TheGetEvent1221Info.ps1Script...................................................275 TheProvisionExchangeUsers.ps1Script..........................................285 Summary....................................................................................292 Index 295 www.it-ebooks.info About the Author With more than nine years of experience in the information technology sector, Tyson Kopczynskihas become a specialist in Active Directory, Group Policy, Windows scripting, Windows Rights Management Services, PKI, and information technology security prac- tices. Tyson has been a contributing author for such books as Microsoft Internet Security and Acceleration (ISA) Server 2004 UnleashedandMicrosoft Windows Server 2003 Unleashed (R2 Edition). In addition, he has written detailed technical papers and guides covering the various in-the-field technologies he works with extensively. As a consultant at Convergent Computing (CCO), Tyson has been able to work with the next generation of Microsoft technologies since their inception and played a key role in expanding scripting and devel- opment practices at CCO. Tyson also holds the SANS Security Essentials Certification (GSEC), Microsoft Certified Systems Engineer (MCSE) Security certification, CompTIA Security+ certification, and SANS Certified Incident Handler (GCIH) certification. www.it-ebooks.info