Windows Powershell 2.0 Administrator's Pocket Consultant
Total Page:16
File Type:pdf, Size:1020Kb
Windows PowerShell 2.0 ™ William R. Stanek Author and Series Editor Administrator’s Pocket Consultant PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2009 by William Stanek 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: 2009927478 Printed and bound in the United States of America. 1 2 3 4 5 6 7 8 9 QWE 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, DirectX, Halo, Halo Wars, MS, MSDN, Visual C#, Visual Studio, Windows, Windows Live, Windows Media, Windows Vista, Xbox, Xbox 360, Xbox LIVE, XNA and Zune are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. 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: Denise Bankaitis Editorial Production: Macmillan Publishing Solutions Technical Reviewer: Technical Reviewer: LJ Zacker; Technical Review services provided by Content Master, a member of CM Group, Ltd. Cover: Tom Draper Design Body Part No. X15-58129 Contents at a Glance Introduction xiii CHAPTER 1 Introducing Windows PowerShell 1 CHAPTER 2 Getting the Most from Windows PowerShell 25 CHAPTER 3 Managing Your Windows PowerShell Environment 53 CHAPTER 4 Using Sessions, Jobs, and Remoting 85 CHAPTER 5 Navigating Core Windows PowerShell Structures 109 CHAPTER 6 Mastering Aliases, Functions, and Objects 167 CHAPTER 7 Managing Computers with Commands and Scripts 213 CHAPTER 8 Managing Roles, Role Services, and Features 237 CHAPTER 9 Inventorying and Evaluating Windows Systems 253 CHAPTER 10 Managing File Systems, Security, and Auditing 281 CHAPTER 11 Managing Shares, Printers, and TCP/IP Networking 309 CHAPTER 12 Managing and Securing the Registry 335 CHAPTER 13 Monitoring and Optimizing Windows Systems 361 CHAPTER 14 Fine-Tuning System Performance 409 Index 445 Contents Introduction xiii Chapter 1 Introducing Windows PowerShell 1 Getting Started with Windows PowerShell ....................2 Running Windows PowerShell ...............................3 Using the Windows PowerShell Console 3 Using the Windows PowerShell ISE 5 Configuring Windows PowerShell Console Properties 7 Working with the Command History 8 Working with Cmdlets and Scripts ...........................9 Using Cmdlets 9 Using Cmdlet Parameters 14 Using External Commands 16 Using Scripts 17 Chapter 2 Getting the Most from Windows PowerShell 25 Initializing the Environment ................................26 Passing Startup Parameters 26 Invoking Windows PowerShell 28 Using –Command to Run Commands 28 Using –File to Run Scripts 30 Using Nested Consoles 30 Understanding Command Input, Parsing, and Output .........31 Basic Line Editing 31 How Parsing Works 33 Parsing Assigned Values 34 Parsing Exceptions 36 Output from Parsing 37 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 v Writing and Formatting Output ............................37 Using Formatting Cmdlets 38 Writing to Output Streams 45 Rendering and Finalizing the Output 49 More on Redirecting Input, Output, and Error 51 Chapter 3 Managing Your Windows PowerShell Environment 53 Using Profiles .............................................53 Creating Profiles 55 Understanding Execution Order 56 Working with the Command Path 57 Navigating Windows PowerShell Extensions .................60 Working with Windows PowerShell Extensions 60 Using Snap-ins 62 Using Providers 64 Navigating and Using Provider Drives 72 Using Modules 77 PowerShell Extensions for Exchange Server and SQL Server 82 Chapter 4 Using Sessions, Jobs, and Remoting 85 Enabling Remote Commands ...............................85 Executing Remote Commands ..............................87 Understanding Remote Execution 88 Commands for Remoting 88 Invoking Remote Commands 92 Establishing Remote Sessions ...............................94 Invoking Sessions 94 Understanding Remote Execution and Object Serialization 96 Establishing Remote Background Jobs .......................97 Using Background Jobs 97 Starting Jobs in Interactive Sessions 100 Running Jobs Noninteractively 104 Working Remotely Without WinRM ........................107 vi Contents Chapter 5 Navigating Core Windows PowerShell Structures 109 Working with Expressions and Operators ...................109 Arithmetic, Grouping, and Assignment Operators 110 Comparison Operators 113 Other Operators 121 Working with Variables and Values .........................122 Variable Essentials 123 Assigning and Converting Data Types 128 Managing Variable Scopes 135 Automatic, Preference, and Environment Variables 138 Working with Strings .....................................148 Single-Quoted and Double-Quoted Strings 148 Escape Codes and Wildcards 150 Multiline Strings 152 String Operators 154 Working with Arrays and Collections .......................159 Creating and Using One-Dimensional Arrays 160 Using the Cast Array Structure 162 Assigning and Removing Values 163 Using Strict Types in Arrays 164 Using Multidimensional Arrays 165 Chapter 6 Mastering Aliases, Functions, and Objects 167 Creating and Using Aliases ................................168 Using the Built-In Aliases 168 Creating Aliases 172 Importing and Exporting Aliases 174 Creating and Using Functions .............................175 Creating Functions 175 Using Extended Functions 177 Using Filter Functions 178 Digging Deeper into Functions 179 Examining Function Definitions 181 Using the Built-In Functions 181 Contents vii Working with Objects ....................................185 Object Essentials 185 Object Methods and Properties 188 Object Types 190 Digging Deeper into Objects 194 Working with COM and .NET Framework Objects ............197 Creating and Using COM Objects 197 Working with .NET Framework Classes and Objects 202 Working with WMI Objects and Queries ....................206 Chapter 7 Managing Computers with Commands and Scripts 213 Getting More from Your Scripts and Profiles ................213 Creating Transcripts ......................................216 Creating Transactions ....................................217 Understanding Transactions 217 Using Transactions 220 Common Elements in Scripts ..............................222 Using Comments and Initializing Statements 222 Using Conditional Statements 225 Using Control Loops 231 Chapter 8 Managing Roles, Role Services, and Features 237 Server Manager Essentials ................................238 Server Manager Commands 238 Available Roles and Role Services 239 Available Features 242 Checking Installed Roles, Role Services, and Features ........245 Installing Roles, Role Services, and Features .................247 Adding Roles, Role Services, and Features 247 Handling Configuration Errors and Other Issues 249 Uninstalling Roles, Role Services, and Features ..............250 Removing Roles, Role Services, and Features 250 Handling Removal Errors and Other Issues 252 viii Contents Chapter 9 Inventorying and Evaluating Windows Systems 253 Getting Basic System Information ..........................253 Determining the Current User, Domain, and Computer Name 254 Determining and Setting the Date and Time 255 Specifying Authentication Credentials 257 Examining the System Configuration and the Working Environment. 257 Determining Windows Updates and Service Packs 258 Obtaining Detailed System Information 261 Determining Available Users and Groups 265 Evaluating System Hardware ..............................267 Checking Firmware Versions and Status 267 Checking Physical Memory and Processors 269 Checking Hard Disks and Partitions 271 Checking and Managing Device Drivers 275 Digging In Even More 279 Chapter 10 Managing File Systems, Security, and Auditing 281 Managing PowerShell Drives, Directories, and Files ..........281 Adding and Removing PowerShell Drives 282 Creating and Managing Directories and Files 283 Working with File Contents ...............................286 Commands for Managing File Contents 286 Reading and Writing File Content 288 Accessing