Powershell in action
Licensed to Marco Tarantino
Licensed to Marco Tarantino
Windows PowerShell in Action
BRUCE PAYETTE
MANNING
Greenwich (74° w. long.)
Licensed to Marco Tarantino
For online information and ordering of this and other Manning books, please go to www.manning.com. The publisher offers discounts on this book when ordered in quantity. For moreinformation, please contact: Special Sales Department Manning Publications Sound View Court 3B Greenwich, CT 06830
Fax: (609) 877-8256 email: orders@manning.com
©2007 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, withoutprior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’spolicy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Manning Publications Co. Sound View Court 3B Greenwich, CT 06830
Copyeditor: Benjamin Berg Typesetter: Gordan Salinovic Cover designer: Leslie Haimes
ISBN 1932394-90-7 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 11 10 09 08 07
Licensed to Marco TarantinoTo my wife, Tina, for all her love and support
Licensed to Marco Tarantino
Licensed to Marco Tarantino
brief contents
Part 1 Learning PowerShell 1
1 Welcome to PowerShell 3 2 The basics 25 3 Working with types 55 4 Operators and expressions 87 115
5 Advanced operators and variables 6 Flow control in scripts 147
7 Functions and scripts 177 8 Scriptblocks and objects 214 9Errors, exceptions, and script debugging 251
Part 2
Using PowerShell 295
10 Processing text, files, and XML 297 11 Getting fancy—.NET and WinForms 12 Windows objects: COM and WMI 13 Security, security, security 440 344
392
vii
Licensed to Marco Tarantino
Licensed to Marco Tarantino
contents
foreword xv preface xvii acknowledgments xix about this book xx
Part 1LEARNING POWERSHELL 1
1 Welcome to PowerShell 3
1.1 What is PowerShell? 5 Shells, command-lines, and scripting languages 5 ✦ Why a new shell? Why now? 7 ✦ The last mile problem 7 1.2 Soul of a new language 8 Learning from history 8 ✦ Leveraging .NET 9 1.3 Brushing up on objects 10 Reviewing object-oriented programming 11 Objects in PowerShell 12 1.4 Dude! Where’s my code? 13 Installing and startingPowerShell 13 ✦ Command editing 15 Command completion 16 ✦ Evaluating basic expressions 17 Processing data 18 1.5 Summary 23
2 The basics 25
2.1 Command concepts and terminology 27 Commands and cmdlets 27 ✦ Command categories 30 Aliases and elastic syntax 34 2.2 Parsing and PowerShell 37 How PowerShell parses 37 ✦ Quoting 38 ✦ Expression mode and command mode parsing 41 ✦ Statement termination43
ix
Licensed to Marco Tarantino
2.3 Pipelines and commands 45 Pipelines and streaming behavior 46 Parameters and parameter binding 47 2.4 Formatting and output 48 The formatting cmdlets 49 ✦ The outputter cmdlets 51 2.5 Summary 54
3 Working with types 55
3.1 Type management in the wild, wild west 55 PowerShell: a type-promiscuous language 56 The type system and type adaptation 58 3.2Basic types and literals 60 Strings 60 ✦ Numbers and numeric literals 64 ✦ Collections: dictionaries and hashtables 66 ✦ Collections: arrays and sequences 71 ✦ Type literals 75 3.3 Type conversions 79 How type conversion works 79 ✦ PowerShell’s type-conversion algorithm 82 ✦ Special type conversions in parameter binding 85 3.4 Summary 86
4 Operators and expressions
87
4.1 Arithmetic...
Regístrate para leer el documento completo.