Microsoft® Windows® PowerShell 2.0 Programming for the Absolute Beginner, Second Edition Jerry Lee Ford, Jr. Course Technology PTR A part of Cengage Learning Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States Microsoft® Windows® PowerShell 2.0 © 2009 Course Technology, a part of Cengage Learning. Programming for the Absolute ALL RIGHTS RESERVED. No part of this work covered by the copyright Beginner, Second Edition: herein may be reproduced, transmitted, stored, or used in any form or by Jerry Lee Ford, Jr. any means graphic, electronic, or mechanical, including but not limited to Publisher and General Manager, Course photocopying, recording, scanning, digitizing, taping, Web distribution, Technology PTR: Stacy L. Hiquet information networks, or information storage and retrieval systems, except as permitted under Section 107 or 108 of the 1976 United States Copyright Associate Director of Marketing: Act, without the prior written permission of the publisher. Sarah Panella Manager of Editorial Services: For product information and technology assistance, contact us at Heather Talbot Cengage Learning Customer & Sales Support, 1-800-354-9706 Marketing Manager: Mark Hughes For permission to use material from this text or product, submit all Acquisitions Editor: Mitzi Koontz requests online at cengage.com/permissions Further permissions questions can be emailed to [email protected] Project Editor: Jenny Davidson Technical Reviewer: Keith Davenport Microsoft and Windows are registered trademarks of Microsoft Corporation in the United States and/or other countries. Interior Layout Tech: Value Chain All other trademarks are the property of their respective owners. Cover Designer: Mike Tanamachi Library of Congress Control Number: 2008941543 Indexer: Kevin Broccoli ISBN-13: 978-1-59863-899-8 Proofreader: Sara Gullion ISBN-10: 1-59863-899-8 eISBN-10: 1-43545-443-x Course Technology 20 Channel Center Boston, MA 02210 USA Cengage Learning is a leading provider of customized learning solutions with office locations around the globe, including Singapore, the United Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local office at: international.cengage.com/region Cengage Learning products are represented in Canada by Nelson Education, Ltd. For your lifelong learning solutions, visit courseptr.com Visit our corporate website at cengage.com Printed in the United States of America 1 2 3 4 5 6 7 11 10 09 To my mother and father for always being there, and to my wonderful children, Alexander, William, and Molly, and my beautiful wife, Mary. A CKNOWLEDGMENTS T he new edition of this book represents the culmination of hard work from a number of individuals to whom I owe many thanks. For starters, there is Mitzi Koontz, for helping me get this book started and for her support as acquisition editor. I also owe a special debt of gratitude to Jenny Davidson, who served as the book’s project editor and worked hard to help keep me straight and ensured that everything came together like it was supposed to. Thanks also go out to Keith Davenport, who served as technical editor for both the first and second edition of this book. Keith provided invaluable insight, guidance, and advice. Finally, I’d like to thank everyone else at Course Technology PTR for all their con- tributions and hard work. A A BOUT THE UTHOR J erry Lee Ford, Jr. is an author, educator, and an IT professional with over 20 years of experience in information technology, including roles as an automation analyst, technical manager, technical support analyst, au- tomation engineer, and security analyst. He is the author of 30 books and co-author of two additional books. His published works include Perl Programming for the Absolute Beginner, Ruby Programming for the Absolute Beginner, Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner, Microsoft WSH & VBScript Programming for the Absolute Beginner, Microsoft VBScript Professional Projects, Microsoft Windows Shell Scripting and WSH Administrator’s Guide, Microsoft Windows Shell Script Programming for the Absolute Beginner, and Microsoft Windows XP Professional Adminis- trator’s Guide. Jerry has a master’s degree in business administration from Virginia Commonwealth University in Richmond, Virginia, and he has over five years of experience as an adjunct instructor teaching networking courses in information technology. This page intentionally left blank Table of Contents Part I Windows PowerShell Basics.................................... 1 Chapter 1 Introducing Windows PowerShell........................ 3 Project Preview: The Knock Knock Joke Game............................................................... 4 Getting to Know Windows PowerShell............................................................................ 6 A Little History Lesson.................................................................................................. 7 Integration with .NET................................................................................................... 8 PowerShell Versus cmd.exe........................................................................................ 9 What’s New in Windows PowerShell 2.0?..................................................................... 10 Notable Windows PowerShell Changes................................................................. 10 New Windows PowerShell Features....................................................................... 10 Uninstalling PowerShell 1.0.............................................................................................. 12 Installing and Configuring Windows PowerShell...................................................... 13 Configuring Windows Remote Management............................................................... 15 Interacting with the PowerShell Command Prompt................................................. 17 Starting a New PowerShell Session........................................................................ 17 Executing PowerShell Cmdlets................................................................................ 18 Other Types of Commands........................................................................................ 20 A Short PowerShell Workout................................................................................... 20 Windows PowerShell Scripting........................................................................................ 25 Simplifying PowerShell Script Execution..................................................................... 25 Back to the Knock Knock Joke Game.............................................................................. 26 Designing the Game................................................................................................... 27 The Final Result........................................................................................................... 31 Summary................................................................................................................................ 33 Chapter 2 Interacting with the Windows PowerShell Command Line and Graphical Environment....... 35 Project Preview: The Story of the Three Amigos.......................................................... 36 Accessing Windows PowerShell....................................................................................... 39 Customizing the Windows PowerShell Working Environment............................. 40 Customizing Windows PowerShell Shortcuts..................................................... 40 Configuring the Windows Command Console............................................................ 41 viii Microsoft Windows PowerShell 2.0 Programming for the Absolute Beginner, Second Edition Windows Command Console Customization Options...................................... 42 Scripting Console Configuration............................................................................ 46 Windows Command Console Editing Features................................................... 49 Windows PowerShell Edit Enhancements.................................................................... 50 Tab Completion........................................................................................................... 50 The Get-History Cmdlet............................................................................................. 53 Working with Graphical Windows PowerShell........................................................... 54 Executing Windows PowerShell Commands....................................................... 55 Configuring the Output Pane.................................................................................. 55 Editing and Executing PowerShell Scripts........................................................... 57 Creating Additional Runspaces............................................................................... 59 Accessing the Windows PowerShell Graphical Help Files................................ 60 Navigating Hierarchical Data Stores.............................................................................. 62 Back to The Story of the Three Amigos.......................................................................... 66 Creating a New Script................................................................................................ 67 Declaring Script Variables........................................................................................ 68 Displaying the Introduction..................................................................................... 68 Providing Player Instructions.................................................................................. 69 Prompting the Player for Input............................................................................... 70 Collecting Additional Inputs.................................................................................... 71 Displaying the Story’s Opening............................................................................... 73 Displaying the Rest of the Story.............................................................................. 74 Summary................................................................................................................................ 77 Chapter 3 Object-Based Scripting with .NET........................ 79 Project Preview: The PowerShell Fortune Teller Game............................................. 80 Additional PowerShell Customization Techniques.................................................... 82 The Microsoft .NET Framework........................................................................................ 84 Key .NET Framework Components......................................................................... 85 The .NET Class Library................................................................................................ 85 The Common Language Runtime........................................................................... 86 Accessing .NET Framework Resources................................................................... 86 Executing Cmdlets............................................................................................................... 90 Executing Background Jobs.............................................................................................. 98 Creating and Executing Background Jobs.......................................................... 100 Retrieving Information about Background Jobs............................................... 101 Retrieving Background Job Output...................................................................... 102 Waiting for a Background to Complete.............................................................. 103 Stopping Background Job Execution................................................................... 104 Deleting a Background Job..................................................................................... 105 Windows PowerShell Plumbing.................................................................................... 106 Working with Aliases....................................................................................................... 109 Back to the PowerShell Fortune Teller Game............................................................ 111 Contents ix Designing the Game................................................................................................. 111 Creating a New PowerShell Script........................................................................ 112 Declaring and Initializing Variables.................................................................... 112 Displaying the Welcome Screen............................................................................ 113 Displaying Game Instructions............................................................................... 114 Controlling Gameplay.............................................................................................. 115 Displaying the Closing Screen............................................................................... 118 Summary.............................................................................................................................. 119 Part II Learning How to Write PowerShell Scripts....... 121 Chapter 4 Working with Variables, Arrays, and Hashes. . 123 Project Preview: The Seinfeld Trivia Quiz................................................................... 124 Windows PowerShell Language Features.................................................................... 126 Windows PowerShell Reserved Words................................................................ 126 Escape Characters...................................................................................................... 127 String Manipulation................................................................................................. 129 Storing and Retrieving Data........................................................................................... 132 Variables...................................................................................................................... 133 Arrays............................................................................................................................ 139 Associative Arrays...................................................................................................... 143 Back to the Seinfeld Trivia Quiz.................................................................................... 145 Designing the Game................................................................................................. 145 The Final Result......................................................................................................... 156 Summary.............................................................................................................................. 156 Chapter 5 Implementing Conditional Logic......................... 157 Project Preview: The Guess My Number Game.......................................................... 158 Comparing Values............................................................................................................. 160 Combining Pipelines and Operators............................................................................ 162 Implementing Conditional Logic.................................................................................. 162 Comparing Data Using the if Statement............................................................ 163 Making Multiple Comparisons Using the switch Statement........................ 168 Windows PowerShell Operators.................................................................................... 170 Comparison Operators............................................................................................. 170 Logical Operators....................................................................................................... 171 String Comparison Operators................................................................................ 172 Back to the Guess My Number Game........................................................................... 173 Designing the Game................................................................................................. 173 The Final Result......................................................................................................... 181 Summary.............................................................................................................................. 185
Description: