Powershell in Depth

Powershell in Depth

Covers PowerShell 3.0 An administrator’s guide Don Jones Richard Siddaway Jeffery Hicks MANNING www.it-ebooks.info PowerShell in Depth www.it-ebooks.info www.it-ebooks.info PowerShell in Depth AN ADMINISTRATOR’S GUIDE DON JONES RICHARD SIDDAWAY JEFFERY HICKS MANNING SHELTER ISLAND www.it-ebooks.info For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email: [email protected] ©2013 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, without prior 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’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editor: Cynthia Kane 20 Baldwin Road Copyeditor: Liz Welch PO Box 261 Technical proofreader: Aleksandar Nikolic Shelter Island, NY 11964 Proofreader: Linda Recktenwald Typesetter: Dennis Dalinnik Cover designer: Marija Tudor ISBN: 9781617290558 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 19 18 17 16 15 14 13 www.it-ebooks.info brief contents PART 1 POWERSHELL FUNDAMENTALS . .....................................1 1 ■ Introduction 3 2 ■ PowerShell hosts 7 3 ■ Using the PowerShell help system 17 4 ■ The basics of PowerShell syntax 29 5 ■ Working with PSSnapins and modules 39 6 ■ Operators 46 7 ■ Working with objects 60 8 ■ The PowerShell pipeline 93 9 ■ Formatting 111 PART 2 POWERSHELL MANAGEMENT ......................................127 10 ■ PowerShell Remoting 129 11 ■ Background jobs and scheduling 160 12 ■ Working with credentials 174 13 ■ Regular expressions 184 14 ■ Working with HTML and XML data 196 v www.it-ebooks.info vi BRIEF CONTENTS 15 ■ PSDrives and PSProviders 210 16 ■ Variables, arrays, hash tables, and scriptblocks 224 17 ■ PowerShell security 244 18 ■ Advanced PowerShell syntax 257 PART 3 POWERSHELL SCRIPTING AND AUTOMATION ...............275 19 ■ PowerShell’s scripting language 277 20 ■ Basic scripts and functions 291 21 ■ Creating objects for output 301 22 ■ Scope 317 23 ■ PowerShell workflows 332 24 ■ Advanced syntax for scripts and functions 359 25 ■ Script modules and manifest modules 379 26 ■ Custom formatting views 391 27 ■ Custom type extensions 403 28 ■ Data language and internationalization 417 29 ■ Writing help 429 30 ■ Error handling techniques 435 31 ■ Debugging tools and techniques 447 32 ■ Functions that work like cmdlets 466 33 ■ Tips and tricks for creating reports 485 PART 4 ADVANCED POWERSHELL ...........................................495 34 ■ Working with the Component Object Model (COM) 497 35 ■ Working with .NET Framework objects 505 36 ■ Accessing databases 517 37 ■ Proxy functions 525 38 ■ Building a GUI 538 39 ■ WMI and CIM 557 40 ■ Best practices 584 www.it-ebooks.info contents preface xxi acknowledgments xxiii about this book xxv about the authors xxvii about the cover illustration xxix PART 1 POWERSHELL FUNDAMENTALS. ..........................1 Introduction 3 1 1.1 Who this book is for 3 1.2 What this book will teach you 4 1.3 What this book won’t teach you 4 1.4 Where we drew the line 5 1.5 Beyond PowerShell 5 1.6 Ready? 6 PowerShell hosts 7 2 2.1 32-bit vs. 64-bit, and administrator vs. not 8 2.2 The console 10 2.3 The PowerShell ISE 12 vii www.it-ebooks.info viii CONTENTS 2.4 Command history buffer vs. PowerShell’s history 15 2.5 Transcripts 16 2.6 Summary 16 Using the PowerShell help system 17 3 3.1 The help commands 17 3.2 Where’s the help? 18 3.3 Using the help 20 3.4 “About” help files 23 3.5 Provider help 24 3.6 Interpreting command help 25 3.7 Common parameters 27 3.8 Summary 28 The basics of PowerShell syntax 29 4 4.1 Commands 30 Aliases: nicknames for commands 31 ■ Command name tab completion 32 4.2 Parameters 32 Truncating parameter names 34 ■ Parameter name tab completion 35 4.3 Typing trick: line continuation 35 4.4 Parenthetical commands and expressions 36 4.5 Script blocks 37 4.6 Summary 38 Working with PSSnapins and modules 39 5 5.1 There’s only one shell 39 5.2 PSSnapins vs. modules 40 5.3 Loading, autoloading, and profiles 41 5.4 Using extensions 41 Discovering extensions 41 ■ Loading extensions 43 Discovering extensions’ additions 43 ■ Managing extensions 44 5.5 Command name conflicts 44 5.6 Managing module autoloading 45 5.7 Summary 45 www.it-ebooks.info CONTENTS ix Operators 46 6 6.1 Logical and comparison operators 47 The –contains operator 48 ■ The -in and -notin operators 49 Boolean, or logical, operators 50 ■ Bitwise operators 51 6.2 Arithmetic operators 53 6.3 Other operators 55 String and array manipulation operators 55 Object type operators 56 ■ Format operator 57 Miscellaneous operators 58 6.4 Summary 59 Working with objects 60 7 7.1 Introduction to objects 61 7.2 Members: properties, methods, and events 63 7.3 Sorting objects 68 7.4 Selecting objects 69 Use 1: choosing properties 70 ■ Use 2: choosing a subset of objects 71 ■ Use 3: making custom properties 73 Use 4: extracting and expanding properties 75 Use 5: choosing properties and a subset of objects 79 7.5 Filtering objects 79 Simplified syntax 79 ■ Full syntax 81 7.6 Grouping objects 81 7.7 Measuring objects 83 7.8 Enumerating objects 84 Full syntax 85 ■ Simplified syntax 85 7.9 Importing, exporting, and converting objects 86 7.10 Comparing objects 90 7.11 Summary 92 The PowerShell pipeline 93 8 8.1 How the pipeline works 93 The old way of piping 94 ■ The PowerShell way of piping 95 8.2 Parameter binding ByValue 96 8.3 Pipeline binding ByPropertyName 98 8.4 Troubleshooting parameter binding 104 www.it-ebooks.info x CONTENTS 8.5 When parameter binding lets you down 109 8.6 The pipeline with external commands 110 8.7 Summary 110 Formatting 111 9 9.1 The time to format 111 9.2 The formatting system 113 Is there a predefined view? 113 ■ What properties should be displayed? 113 ■ List or table? 114 9.3 The Format cmdlets 114 Formatting wide lists 114 ■ Formatting tables 115 Formatting lists 120 ■ Same objects, different formats 122 9.4 Eliminating confusion and “gotchas” 122 Formatting is the end of the line 122 ■ Select or format? 123 Format, out, export—which? 124 9.5 Summary 125 PART 2 POWERSHELL MANAGEMENT ..........................127 PowerShell Remoting 129 10 10.1 The many forms of remote control 130 10.2 Remoting overview 130 Authentication 131 ■ Firewalls and security 132 10.3 Using Remoting 132 Enabling Remoting 132 ■ 1-to-1 Remoting 133 1-to-many Remoting 134 ■ Remoting caveats 136 Remoting options 138 10.4 PSSessions 140 Creating a persistent session 140 ■ Using a session 140 Managing sessions 141 ■ Disconnecting and reconnecting sessions 141 10.5 Advanced session techniques 144 Session parameters 144 ■ Session options 145 10.6 Creating a custom endpoint 145 Custom endpoints for delegated administration 147 www.it-ebooks.info CONTENTS xi 10.7 Connecting to nondefault endpoints 148 10.8 Enabling the “second hop” 149 10.9 Setting up WinRM listeners 150 10.10 Other configuration scenarios 152 Cross-domain Remoting 152 ■ Quotas 153 ■ Configuring on a remote machine 154 ■ Key WinRM configuration settings 154 Adding a machine to your Trusted Hosts list 155 ■ Using Group Policy to configure Remoting 156 10.11 Implicit Remoting 157 10.12 Summary 159 Background jobs and scheduling 160 11 11.1 Remoting-based jobs 160 Starting jobs 161 ■ Checking job status 162 ■ Working with child jobs 162 ■ Waiting for a job 164 ■ Stopping jobs 164 Getting job results 164 ■ Removing jobs 165 Investigating failed jobs 166 11.2 WMI jobs 166 11.3 Scheduled jobs 167 Scheduled jobs overview 168 ■ Creating a scheduled job 168 Managing scheduled jobs 169 ■ Working with scheduled job results 170 11.4 Job processes 170 Jobs created with Start-Job 171 ■ Jobs created with Invoke-Command 172 ■ Jobs created through the WMI cmdlets 173 ■ Jobs created through the scheduler 173 11.5 Summary 173 Working with credentials 174 12 12.1 About credentials 175 12.2 Using credentials 178 12.3 Crazy credentials ideas 179 Packaging your script 179 ■ Saving a credential object 179 Creating a credential without the GUI 181 Supporting credentials in your script 181 12.4 Summary 183 www.it-ebooks.info xii CONTENTS Regular expressions 184 13 13.1 Basic regular expression syntax 185 13.2 The –match operator 188 13.3 The select-string cmdlet 190 13.4 Switch statement 190 13.5 The REGEX object 192 13.6 Summary 195 Working with HTML and XML data 196 14 14.1 Working with HTML 196 Retrieving an HTML page 197 ■ Working with the HTML results 198 ■ Practical example 201 Creating HTML output 202 14.2 Working with XML 206 Using XML to persist data 206 ■ Reading arbitrary XML data 207 ■ Creating XML data and files 208 Using XML XPath queries

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    633 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us