Bruce G. Payette9781932394900, 1932394907
–Jeffrey Snover, from the Foreword
“The nuances of PowerShell from the lead language designer himself! Excellent content and easy readability!
–Keith Hill, Software Architect
“I love this book!”
–Scott Hanselman ComputerZen.com
PowerShell replaces cobbled-together assemblies of third-party management tools with an elegant programming language and a powerful scripting shell for the Windows environment. In the tradition of Manning’s ground breaking “In Action” series, this book comes from right from the source. Windows PowerShell in Action shows you how to build scripts and utilities to automate system tasks or create powerful system management tools to handle the day-to-day tasks that drive a Windows administrator’s life. Because it’s based on the .NET platform, PowerShell is also a powerful tool for developers and power users.
Windows PowerShell in Action was written by Bruce Payette, one of the founding members of the Windows PowerShell team, co-designer of the PowerShell language and the principal author of the PowerShell language implementation. The book enables you to get the most out of the PowerShell environment. Using many examples, both small and large, this book illustrates the features of the language and environment and shows how to compose those features into solutions, quickly and effectively.
This book is designed for anyone who wants to learn PowerShell and use it well. Rather than simply being a book of recipes to read and apply, this book gives you the deep knowledge about how PowerShell works and how to apply it
Table of contents :
Windows PowerShell in Action……Page 1
contents……Page 7
foreword……Page 13
freface……Page 15
Part 1 Learning PowerShell……Page 23
Welcome to PowerShell……Page 24
1.1.1 Shells, command-lines, and scripting languages……Page 26
1.1.3 The last mile problem……Page 28
1.2.1 Learning from history……Page 29
1.2.2 Leveraging .NET……Page 30
1.3 Brushing up on objects……Page 31
1.3.1 Reviewing object-oriented programming……Page 32
1.3.2 Objects in PowerShell……Page 33
1.4.1 Installing and starting PowerShell……Page 34
1.4.2 Command editing……Page 36
1.4.3 Command completion……Page 37
1.4.4 Evaluating basic expressions……Page 38
1.4.5 Processing data……Page 39
1.5 Summary……Page 44
The basics……Page 46
2.1.1 Commands and cmdlets……Page 48
2.1.2 Command categories……Page 51
2.1.3 Aliases and elastic syntax……Page 55
2.2.1 How PowerShell parses……Page 58
2.2.2 Quoting……Page 59
2.2.3 Expression mode and command mode parsing……Page 62
2.2.4 Statement termination……Page 64
2.3 Pipelines and commands……Page 66
2.3.1 Pipelines and streaming behavior……Page 67
2.3.2 Parameters and parameter binding……Page 68
2.4 Formatting and output……Page 69
2.4.1 The formatting cmdlets……Page 70
2.4.2 The outputter cmdlets……Page 72
2.5 Summary……Page 75
3.1 Type management in the wild, wild west……Page 76
3.1.1 PowerShell: a type-promiscuous language……Page 77
3.1.2 The type system and type adaptation……Page 79
3.2.1 Strings……Page 81
3.2.2 Numbers and numeric literals……Page 85
3.2.3 Collections: dictionaries and hashtables……Page 87
3.2.4 Collections: arrays and sequences……Page 92
3.2.5 Type literals……Page 96
3.3.1 How type conversion works……Page 100
3.3.2 PowerShell’s type-conversion algorithm……Page 103
3.3.3 Special type conversions in parameter binding……Page 106
3.4 Summary……Page 107
Operators and expressions……Page 108
4.1.1 The addition operator……Page 110
4.1.2 The multiplication operator……Page 113
4.1.3 Subtraction, division, and the modulus operator……Page 115
4.2 The assignment operators……Page 117
4.2.1 Multiple assignments……Page 118
4.2.2 Multiple assignments with type qualifiers……Page 119
4.2.3 Assignment operations as value expressions……Page 121
4.3 Comparison operators……Page 122
4.3.1 Scalar comparisons……Page 123
4.3.2 Using comparison operators with collections……Page 126
4.4.1 Wildcard patterns……Page 128
4.4.2 Regular expressions……Page 129
4.6 Summary……Page 134
5.1 Operators for working with types……Page 136
5.2 The unary operators……Page 138
5.3 Grouping, subexpressions, and array subexpressions……Page 140
5.4.1 The comma operator “,”……Page 144
5.4.2 The range operator……Page 147
5.4.3 Array indexing……Page 148
5.5 Property and method operators……Page 153
5.5.1 The “.” operator……Page 154
5.5.2 Static methods and the “::” operator……Page 157
5.6 The PowerShell format operator -F……Page 158
5.7 Redirection and the redirection operators……Page 159
5.8 Variables……Page 162
5.9 Summary……Page 166
Flow control in scripts……Page 168
6.1 Using the if/elseif/else statement……Page 169
6.2 The while loop……Page 172
6.3 The do/while loop……Page 173
6.4 The for loop……Page 174
6.5 The foreach loop……Page 176
6.6 Labels, break, and continue……Page 180
6.7.1 Basic use of the PowerShell switch statement……Page 182
6.7.2 Using wildcard patterns with the switch statement……Page 184
6.7.3 Using regular expressions with the switch statement……Page 185
6.7.4 Processing files with the switch statement……Page 188
6.7.5 Using the $switch loop enumerator in the switch statement……Page 189
6.8 Flow control using cmdlets……Page 190
6.8.1 The Foreach-Object cmdlet……Page 191
6.8.2 The Where-Object cmdlet……Page 194
6.9 The value of statements……Page 196
6.10 Summary……Page 197
Functions and scripts……Page 198
7.1 Function basics……Page 199
7.2 Formal parameters and the param statement……Page 202
7.2.1 Specifying parameter types……Page 204
7.2.2 Handling variable numbers of arguments……Page 206
7.2.3 Initializing function parameters……Page 207
7.2.4 Using switch parameters to define flags……Page 209
7.2.5 Variables and scoping rules……Page 211
7.3 Returning values from functions……Page 214
7.3.1 Debugging function output……Page 217
7.3.2 The return statement……Page 219
7.4 Using functions in a pipeline……Page 220
7.4.1 Filters and functions……Page 222
7.4.2 Functions as cmdlets……Page 223
7.5 Managing functions……Page 225
7.6 Scripts at long last……Page 226
7.6.1 Passing arguments to scripts……Page 228
7.6.3 Scopes and scripts……Page 229
7.6.4 Exiting scripts and the exit statement……Page 230
7.6.5 Dotting scripts and functions……Page 231
7.7 Summary……Page 233
Scriptblocks and objects……Page 235
8.1 Scriptblock basics……Page 236
8.1.1 Invoking commands……Page 237
8.1.2 Getting CommandInfo objects……Page 238
8.1.3 The ScriptBlock literal……Page 240
8.1.4 Defining functions at runtime……Page 241
8.2.1 Looking at members……Page 243
8.2.2 Synthetic members……Page 244
8.2.3 Using Add-Member to extend objects……Page 245
8.2.4 Using the select-object cmdlet……Page 251
8.3 A closer look at the type-system plumbing……Page 254
8.3.1 Adding a property……Page 256
8.3.2 Shadowing an existing property……Page 257
8.4.1 Little languages……Page 258
8.4.2 Adding a CustomClass keyword to PowerShell……Page 259
8.5 Type extension……Page 264
8.6.1 The Invoke-Expression cmdlet……Page 266
8.6.2 The ExecutionContext variable……Page 267
8.6.3 Creating functions using the function: drive……Page 269
8.7 Summary……Page 270
Errors, exceptions, and script debugging……Page 272
9.1 Error handling……Page 273
9.1.1 ErrorRecords and the error stream……Page 274
9.1.2 The $error variable and -ErrorVariable parameter……Page 277
9.1.3 The $? and $LASTEXITCODE variables……Page 280
9.1.4 $ErrorActionPreference and the -ErrorAction parameter……Page 282
9.2.1 The trap statement……Page 286
9.2.2 The throw statement……Page 289
9.3.1 Debugging with the host APIs……Page 291
9.3.3 Tracing statement execution……Page 292
9.3.4 Stepping through statement execution……Page 296
9.3.5 Catching undefined variables with strict mode……Page 297
9.4.1 Suspending a script while in step-mode……Page 298
9.4.2 Creating a breakpoint command……Page 300
9.4.3 The script call stack, or “How did I get here?”……Page 302
9.5.1 The Trace-Command cmdlet……Page 304
9.5.2 Tracing type conversions……Page 306
9.5.3 Tracing parameter binding……Page 308
9.6.1 Examining the event log……Page 312
9.7 Summary……Page 314
Part 2 Using PowerShell……Page 316
Processing text, files, and XML……Page 318
10.1.1 Using System.String to work with text……Page 319
10.1.2 Using regular expressions to manipulate text……Page 325
10.2 File processing……Page 326
10.2.1 Working with PSDrives……Page 328
10.2.2 Working with paths that contain wildcards……Page 330
10.2.3 Reading and writing files……Page 334
10.2.4 Searching files with the Select-String cmdlet……Page 340
10.3.1 Using XML as objects……Page 343
10.3.2 Loading and saving XML files…….Page 347
10.3.3 Processing XML documents in a pipeline……Page 354
10.3.4 Using XPath……Page 355
10.3.5 The Import-Clixml and Export-Clixml cmdlets……Page 360
10.4 Summary……Page 363
Getting fancy- .NET and WinForms……Page 365
11.1.1 .NET basics……Page 366
11.1.2 Working with assemblies……Page 367
11.1.3 Finding types……Page 369
11.1.4 Creating instances of types……Page 371
11.1.5 PowerShell is not C#-A cautionary tale……Page 374
11.1.6 Working with generic types……Page 379
11.2.1 Example: Retrieving a web page……Page 382
11.2.2 Example: Processing an RSS feed……Page 383
11.2.3 Example: Writing a web server in PowerShell……Page 385
11.3.1 WinForms basics……Page 392
11.3.2 Example: “My first form”……Page 393
11.3.3 Example: Simple dialog……Page 395
11.3.4 Example: A WinForms library……Page 397
11.3.5 Example: A simple calculator……Page 400
11.3.6 Example: Displaying data……Page 406
11.3.7 Example: Using the GDI+ to do graphics……Page 408
11.4 Summary……Page 412
Windows objects: COM and WMI……Page 413
12.1 Working with COM in PowerShell……Page 414
12.1.1 Automating Windows with COM……Page 417
12.1.2 Networking, applications, and toys……Page 426
12.1.3 Using the ScriptControl object……Page 436
12.1.4 Issues with COM……Page 438
12.2.1 Exploring WMI-what is it, and why do you care?……Page 442
12.2.2 The Get-WmiObject cmdlet……Page 443
12.2.3 The WMI object adapter……Page 444
12.2.4 WMI shootout-VBScript versus PowerShell……Page 446
12.2.5 The WMI type shortcuts……Page 450
12.2.6 Working with WMI methods……Page 453
12.2.7 Working with WMI events……Page 454
12.2.8 Putting modified WMI objects back……Page 455
12.4 Summary……Page 458
Security, security, security……Page 461
13.1.2 What security is not……Page 462
13.1.3 Perception and security……Page 463
13.2 Security modeling……Page 464
13.2.2 Classifying threats using the STRIDE model……Page 465
13.2.3 Security basics: Threats, assets, and mitigations……Page 466
13.3.1 Secure by default……Page 470
13.3.2 Managing the command path……Page 471
13.3.3 Choosing a script execution policy……Page 472
13.4.1 How public key encryption and one-way hashing work……Page 474
13.4.2 Signing authorities and certificates……Page 475
13.4.3 Creating a self-signed certificate……Page 476
13.4.4 Using a certificate to sign a script……Page 479
13.4.5 Enabling strong private key protection for your certificate……Page 483
13.5.1 Using the SecureString class……Page 486
13.5.2 Working with credentials……Page 489
13.5.3 Avoiding Invoke-Expression……Page 492
13.6 Summary……Page 495
A.1 PowerShell and cmd.exe……Page 497
A.1.1 Basic navigation and file operations……Page 498
A.1.2 Variables and substitution……Page 499
A.1.4 Differences in syntax……Page 501
A.1.6 For loop equivalents……Page 503
A.1.7 Batch files and subroutines……Page 504
A.1.8 Setting the prompt……Page 505
A.1.9 Using doskey in PowerShell……Page 506
A.1.10 Using cmd.exe from PowerShell…….Page 508
A.2.1 Example: Stopping all processes……Page 509
A.2.2 Example: Stopping a filtered list of processes……Page 510
A.2.4 Example: Working with dynamic values……Page 511
A.2.6 Example: Checking for prerelease binaries……Page 512
A.2.8 Example: Inserting text into a string……Page 513
A.3 PowerShell and Perl……Page 514
A.4.1 Calling functions and commands……Page 515
A.4.4 Variables and scoping……Page 516
A.5 PowerShell and VBScript……Page 517
B.1 Getting active directory domain information……Page 520
B.2 Listing installed software features……Page 521
B.3 Retrieving terminal server properties……Page 522
B.4 List hot fixes installed on a machine……Page 523
B.5 Finding machines missing a hot fix……Page 524
B.6 Working with the event log……Page 526
B.6.1 Getting a specific EventLog object……Page 527
B.6.2 The event log as a live object……Page 528
B.6.3 Getting remote event logs……Page 529
B.6.4 Saving event logs……Page 530
B.7 Working with existing utility commands……Page 531
B.8 Working with Active Directory and ADSI……Page 533
B.8.2 Adding a user……Page 534
B.8.3 Adding a group of users……Page 535
B.8.4 Updating user properties……Page 537
B.8.5 Removing users……Page 538
B.9 Joining two sets of data……Page 539
C.1 Statement list……Page 541
C.2.1 Pipeline……Page 542
C.2.3 The switch statement……Page 543
C.2.5 The for and while statements……Page 544
C.2.8 The finally statement……Page 545
C.2.10 Function declarations……Page 546
C.3 Expression……Page 547
C.4 Value……Page 548
C.5 Tokenizer rules……Page 549
index……Page 551
Reviews
There are no reviews yet.