Tyson Kopczynski
Total Page:16
File Type:pdf, Size:1020Kb
www.it-ebooks.info Tyson Kopczynski Windows® PowerShell UNLEASHED 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 system, or transmitted by any means, electronic, mechanical, photocopying, recording, Senior Acquisitions 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 Kopczynski, Tyson. Project Editor Microsoft PowerShell unleashed / Tyson Kopczynski. George E. Nedeff p. cm. ISBN 0-672-32953-0 Copy Editor 1. Microsoft Windows (Computer file) 2. Operating systems (Computers) I. Title. Lisa M. Lord QA76.76.O63K66 2007 Senior Indexer 005.4’46—dc22 Cheryl Lenser 2007008894 Proofreader Printed in the United States of America Water Crest First Printing: Publishing 10090807 4321 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 this information. Use of a term in this book should not be regarded as affecting the Josh Tolle 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. Bulk Sales Book Designer Sams Publishing offers excellent discounts on this book when ordered in quantity for Gary Adair 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