Windows PowerShell in action [2nd ed]
9781935182139, 1935182137
The Barnes & Noble Review 1+1=2. The Earth is round. The Windows command line stinks.It's time to revisit one o
311
113
9MB
English
Pages xxx, 984 pages: illustrations; 24 cm
[997]
Year 2011
Report DMCA / Copyright
DOWNLOAD PDF FILE
Table of contents :
contents......Page 5
PART 1 Learning PowerShell......Page 17
1 Welcome to PowerShell......Page 18
1.1 What is PowerShell?......Page 20
1.1.1 Shells, command lines, and scripting languages......Page 21
1.1.2 Why a new shell? Why now?......Page 22
1.1.3 The last mile problem......Page 23
1.2.1 Learning from history......Page 24
1.2.2 Leveraging .NET......Page 25
1.3.1 Reviewing object-oriented programming......Page 26
1.4 Up and running with PowerShell......Page 28
1.4.3 The PowerShell console host......Page 29
1.4.4 The PowerShell Integrated Scripting Environment......Page 32
1.4.5 Command completion......Page 35
1.5.1 Navigation and basic operations......Page 37
1.5.2 Basic expressions and variables......Page 38
1.5.3 Processing data......Page 40
1.5.4 Flow-control statements......Page 45
1.5.5 Scripts and functions......Page 46
1.5.6 Remoting and the Universal Execution Model......Page 47
1.6 Summary......Page 50
2 Foundations of PowerShell......Page 51
2.1 Getting a sense of the PowerShell language......Page 52
2.2.2 Commands and cmdlets......Page 53
2.2.3 Command categories......Page 57
2.3 Aliases and elastic syntax......Page 62
2.4 Parsing and PowerShell......Page 65
2.4.2 Quoting......Page 66
2.4.3 Expression-mode and command-mode parsing......Page 69
2.4.4 Statement termination......Page 71
2.4.5 Comment syntax in PowerShell......Page 73
2.5 How the pipeline works......Page 75
2.5.1 Pipelines and streaming behavior......Page 76
2.5.2 Parameters and parameter binding......Page 77
2.6.1 The formatting cmdlets......Page 79
2.6.2 The outputter cmdlets......Page 82
2.7 Summary......Page 85
3.1 Type management in the wild, wild West......Page 87
3.1.1 PowerShell: a type-promiscuous language......Page 88
3.1.2 The type system and type adaptation......Page 90
3.2.1 String literals......Page 92
3.2.2 Numbers and numeric literals......Page 97
3.3.1 Creating and inspecting hashtables......Page 100
3.3.2 Modifying and manipulating hashtables......Page 103
3.3.3 Hashtables as reference types......Page 105
3.4.1 Collecting pipeline output as an array......Page 106
3.4.3 Polymorphism in arrays......Page 107
3.4.4 Arrays as reference types......Page 108
3.4.5 Singleton arrays and empty arrays......Page 109
3.5.1 Type name aliases......Page 111
3.5.2 Generic type literals......Page 113
3.5.3 Accessing static members with type literals......Page 114
3.6.1 How type conversion works......Page 116
3.6.2 PowerShell’s type-conversion algorithm......Page 119
3.6.3 Special type conversions in parameter binding......Page 122
3.7 Summary......Page 124
4 Operators and expressions......Page 125
4.1 Arithmetic operators......Page 127
4.1.1 The addition operator......Page 128
4.1.2 The multiplication operator......Page 131
4.1.3 Subtraction, division, and the modulus operator......Page 132
4.2 The assignment operators......Page 134
4.2.1 Multiple assignments......Page 135
4.2.2 Multiple assignments with type qualifiers......Page 136
4.2.3 Assignment operations as value expressions......Page 138
4.3 Comparison operators......Page 139
4.3.1 Scalar comparisons......Page 140
4.3.2 Comparisons and case sensitivity......Page 142
4.3.3 Using comparison operators with collections......Page 144
4.4 Pattern matching and text manipulation......Page 146
4.4.1 Wildcard patterns and the -like operator......Page 147
4.4.2 Regular expressions......Page 148
4.4.3 The -match operator......Page 149
4.4.4 The -replace operator......Page 152
4.4.5 The -join operator......Page 154
4.4.6 The -split operator......Page 158
4.5 Logical and bitwise operators......Page 163
4.6 Summary......Page 165
5 Advanced operators and variables......Page 166
5.1 Operators for working with types......Page 167
5.2 The unary operators......Page 169
5.3 Grouping and subexpressions......Page 172
5.3.1 Subexpressions $( ... )......Page 174
5.3.2 Array subexpressions @( ... )......Page 175
5.4.1 The comma operator......Page 177
5.4.2 The range operator......Page 180
5.4.3 Array indexing and slicing......Page 182
5.4.4 Using the range operator with arrays......Page 185
5.4.5 Working with multidimensional arrays......Page 186
5.5 Property and method operators......Page 188
5.5.1 The dot operator......Page 189
5.5.2 Static methods and the double-colon operator......Page 192
5.5.3 Indirect method invocation......Page 193
5.6 The format operator......Page 194
5.7 Redirection and the redirection operators......Page 196
5.8 Working with variables......Page 199
5.8.1 Creating variables......Page 200
5.8.2 Variable name syntax......Page 201
5.8.3 Working with the variable cmdlets......Page 203
5.8.4 Splatting a variable......Page 208
5.9 Summary......Page 211
6 Flow control in scripts......Page 213
6.1 The conditional statement......Page 215
6.2.1 The while loop......Page 218
6.2.2 The do-while loop......Page 219
6.2.3 The for loop......Page 220
6.2.4 The foreach loop......Page 222
6.3 Labels, break, and continue......Page 227
6.4.1 Basic use of the switch statement......Page 230
6.4.2 Using wildcard patterns with the switch statement......Page 231
6.4.3 Using regular expressions with the switch statement......Page 232
6.4.4 Processing files with the switch statement......Page 236
6.4.5 Using the $switch loop enumerator in the switch statement......Page 237
6.5.1 The ForEach-Object cmdlet......Page 238
6.5.2 The Where-Object cmdlet......Page 243
6.6 Statements as values......Page 246
6.7 A word about performance......Page 248
6.8 Summary......Page 249
7 PowerShell functions......Page 251
7.1.1 Passing arguments using $args......Page 252
7.1.2 Example functions: ql and qs......Page 254
7.1.3 Simplifying $args processing with multiple assignment......Page 255
7.2 Declaring formal parameters for a function......Page 256
7.2.1 Mixing named and positional parameters......Page 257
7.2.2 Adding type constraints to parameters......Page 258
7.2.3 Handling variable numbers of arguments......Page 260
7.2.4 Initializing function parameters with default values......Page 261
7.2.6 Using switch parameters to define command switches......Page 263
7.2.7 Switch parameters vs. Boolean parameters......Page 267
7.3 Returning values from functions......Page 272
7.3.1 Debugging problems in function output......Page 274
7.3.2 The return statement......Page 277
7.4 Using simple functions in a pipeline......Page 278
7.4.1 Filters and functions......Page 280
7.4.2 Functions with begin, process, and end blocks......Page 281
7.5 Managing function definitions in a session......Page 282
7.6 Variable scoping in functions......Page 284
7.6.1 Declaring variables......Page 285
7.6.2 Using variable scope modifiers......Page 287
7.7 Summary......Page 288
8 Advanced functions and scripts......Page 290
8.1.1 Script execution policy......Page 291
8.1.2 Passing arguments to scripts......Page 293
8.1.3 Exiting scripts and the exit statement......Page 295
8.1.4 Scopes and scripts......Page 296
8.1.5 Managing your scripts......Page 299
8.1.6 Running PowerShell scripts from other applications......Page 300
8.2 Writing advanced functions and scripts......Page 302
8.2.1 Specifying script and function attributes......Page 303
8.2.2 The CmdletBinding attribute......Page 304
8.2.3 The OutputType attribute......Page 308
8.2.4 Specifying parameter attributes......Page 311
8.2.5 Creating parameter aliases with the Alias attribute......Page 318
8.2.6 Parameter validation attributes......Page 320
8.3 Dynamic parameters and dynamicParam......Page 326
8.3.1 Steps for adding a dynamic parameter......Page 327
8.4 Documenting functions and scripts......Page 329
8.4.2 Creating manual help content......Page 330
8.4.3 Comment-based help......Page 331
8.4.4 Tags used in documentation comments......Page 333
8.5 Summary......Page 336
9 Using and authoring modules......Page 337
9.1 The role of a module system......Page 338
9.1.2 Module mashups: composing an application......Page 339
9.2 Module basics......Page 340
9.2.2 Modules are single-instance objects......Page 341
9.3.1 Finding modules on the system......Page 342
9.3.2 Loading a module......Page 346
9.3.3 Removing a loaded module......Page 350
9.4 Writing script modules......Page 352
9.4.1 A quick review of scripts......Page 353
9.4.2 Turning a script into a module......Page 355
9.4.3 Controlling member visibility with Export-ModuleMember......Page 358
9.4.4 Installing a module......Page 362
9.4.5 How scopes work in script modules......Page 363
9.4.6 Nested modules......Page 365
9.5 Binary modules......Page 368
9.5.1 Binary modules vs. snap-ins......Page 369
9.5.2 Creating a binary module......Page 370
9.5.3 Nesting binary modules in script modules......Page 372
9.6 Summary......Page 375
10 Module manifests and metadata......Page 376
10.1 Module folder structure......Page 377
10.2 Module manifest structure......Page 378
10.3 Production manifest elements......Page 381
10.3.2 Runtime dependencies......Page 383
10.4 Construction manifest elements......Page 385
10.4.1 The loader manifest elements......Page 386
10.4.2 Module component load order......Page 389
10.5 Content manifest elements......Page 390
10.6 Language restrictions in a manifest......Page 391
10.7.1 The PSModuleInfo object......Page 393
10.7.2 Using the PSModuleInfo methods......Page 397
10.7.3 The defining module versus the calling module......Page 399
10.7.5 Controlling when modules can be unloaded......Page 403
10.7.6 Running an action when a module is removed......Page 404
10.8 Summary......Page 405
11 Metaprogramming with scriptblocks and dynamic code......Page 407
11.1 Scriptblock basics......Page 408
11.1.1 Invoking commands......Page 409
11.1.2 The scriptblock literal......Page 412
11.1.3 Defining functions at runtime......Page 413
11.2.1 Looking at members......Page 415
11.2.2 Using Add-Member to extend objects......Page 417
11.2.3 Adding note properties with New-Object......Page 424
11.3 Using the Select-Object cmdlet......Page 425
11.4.1 Dynamic script modules......Page 427
11.4.2 Closures in PowerShell......Page 429
11.4.3 Creating custom objects from modules......Page 432
11.5.1 How steppable pipelines work......Page 433
11.5.2 Creating a proxy command with steppable pipelines......Page 435
11.6 A closer look at the type-system plumbing......Page 438
11.6.1 Adding a property......Page 440
11.6.2 Shadowing an existing property......Page 442
11.7.2 Adding a CustomClass keyword to PowerShell......Page 443
11.7.3 Type extension......Page 448
11.8.1 The Invoke-Expression cmdlet......Page 451
11.8.3 The ExpandString() method......Page 452
11.8.5 Mechanisms for creating scriptblocks......Page 453
11.8.6 Creating functions using the function: drive......Page 454
11.9.1 Defining a new .NET class: C#......Page 455
11.9.2 Defining a new enum at runtime......Page 457
11.9.3 Dynamic binary modules......Page 458
11.10 Summary......Page 460
12 Remoting and background jobs......Page 462
12.1.1 Commands with built-in remoting......Page 463
12.1.2 The PowerShell remoting subsystem......Page 464
12.1.3 Enabling remoting......Page 465
12.1.4 Additional setup steps for workgroup environments......Page 466
12.1.5 Enabling remoting in the enterprise......Page 467
12.2.1 Basic remoting examples......Page 469
12.2.2 Adding concurrency to the examples......Page 470
12.2.3 Solving a real problem: multimachine monitoring......Page 472
12.3 Sessions and persistent connections......Page 477
12.3.1 Additional session attributes......Page 481
12.3.2 Using the New-PSSession cmdlet......Page 483
12.3.3 Interactive sessions......Page 484
12.3.4 Managing PowerShell sessions......Page 487
12.4 Implicit remoting......Page 488
12.4.1 Using implicit remoting......Page 489
12.4.2 How implicit remoting works......Page 491
12.5 Background jobs in PowerShell......Page 496
12.5.2 Working with the job cmdlets......Page 498
12.5.3 Working with multiple jobs......Page 502
12.5.4 Starting jobs on remote computers......Page 504
12.5.5 Running jobs in existing sessions......Page 507
12.6 Considerations when running commands remotely......Page 508
12.6.2 Profiles and remoting......Page 509
12.6.3 Issues running executables remotely......Page 510
12.6.4 Reading and writing to the console......Page 511
12.6.5 Remote output vs. local output......Page 512
12.6.6 Processor architecture issues......Page 513
12.7 Summary......Page 515
13 Remoting: configuring applications and services......Page 517
13.1.1 The PowerShell remoting protocol stack......Page 518
13.1.2 Using the WSMan cmdlets and providers......Page 524
13.1.3 Authenticating the target computer......Page 526
13.1.4 Authenticating the connecting user......Page 529
13.1.5 Addressing the remoting target......Page 533
13.1.6 Windows version-specific connection issues......Page 535
13.1.7 Managing resource consumption......Page 537
13.2.1 Remote service connection patterns......Page 542
13.2.2 Working with custom configurations......Page 545
13.2.3 Creating a custom configuration......Page 546
13.2.4 Access controls and endpoints......Page 548
13.2.5 Constraining a PowerShell session......Page 550
13.2.6 Creating a constrained execution environment......Page 558
13.3 Summary......Page 566
14 Errors and exceptions......Page 568
14.1 Error handling......Page 569
14.1.1 ErrorRecords and the error stream......Page 570
14.1.2 The $error variable and –ErrorVariable parameter......Page 575
14.1.3 Determining if a command had an error......Page 579
14.1.4 Controlling the actions taken on an error......Page 581
14.2 Dealing with errors that terminate execution......Page 584
14.2.1 The trap statement......Page 585
14.2.2 The try/catch/finally statement......Page 590
14.2.3 The throw statement......Page 593
14.3 Debugging with the host APIs......Page 595
14.3.1 Catching errors with strict mode......Page 597
14.3.2 The Set-StrictMode cmdlet in PowerShell v2......Page 599
14.3.3 Static analysis of scripts......Page 604
14.4.1 Starting the transcript......Page 608
14.4.2 What gets captured in the transcript......Page 610
14.5.1 The EventLog cmdlets......Page 612
14.5.2 Examining the PowerShell event log......Page 618
14.6 Summary......Page 620
15 The PowerShell ISE and debugger......Page 621
15.1.1 Controlling the ISE pane layout......Page 622
15.1.2 Using the ISE editor......Page 625
15.1.3 Executing commands in the ISE......Page 629
15.1.4 Considerations when running scripts in the ISE......Page 631
15.2 Using multiple PowerShell tabs......Page 633
15.2.2 Remote session tabs in PowerShell ISE......Page 634
15.3.1 The $psISE variable......Page 637
15.3.2 Using the Options property......Page 639
15.3.3 Managing tabs and files......Page 640
15.3.4 Working with text panes......Page 644
15.3.5 Adding a custom menu......Page 648
15.4.1 The Set-PSDebug cmdlet......Page 653
15.4.2 Nested prompts and the Suspend operation......Page 658
15.5 The PowerShell v2 debugger......Page 662
15.5.1 The graphical debugger......Page 663
15.6 Command-line debugging......Page 667
15.6.1 Working with breakpoint objects......Page 668
15.6.2 Setting breakpoints on commands......Page 671
15.6.3 Setting breakpoints on variable assignment......Page 672
15.6.4 Debugger limitations and issues......Page 673
15.7 Summary......Page 674
PART 2 Using PowerShell......Page 676
16 Working with files, text, and XML......Page 677
16.1.1 Providers and the core cmdlets......Page 678
16.1.2 Working with PSDrives......Page 679
16.1.3 Working with paths that contain wildcards......Page 681
16.1.4 Suppressing wildcard processing in paths......Page 682
16.1.5 The -LiteralPath parameter......Page 684
16.1.6 The Registry provider......Page 685
16.2 File processing......Page 686
16.2.1 Reading and writing files......Page 688
16.2.2 Writing files......Page 693
16.2.4 Performance caveats with Get-Content......Page 694
16.3.1 Using System.String to work with text......Page 695
16.3.2 Using hashtables to count unique words......Page 698
16.3.3 Using regular expressions to manipulate text......Page 700
16.3.4 Searching files with the Select-String cmdlet......Page 702
16.4.1 Using XML as objects......Page 707
16.4.2 Adding elements to an XML object......Page 709
16.4.3 Loading and saving XML files......Page 711
16.4.4 Processing XML documents in a pipeline......Page 715
16.4.5 Processing XML with XPath......Page 716
16.4.6 A hint of XLinq......Page 723
16.4.7 Rendering objects as XML......Page 725
16.5 Summary......Page 731
17 Extending your reach with .NET......Page 733
17.1.1 .NET basics......Page 734
17.1.2 Working with assemblies......Page 735
17.1.3 Finding types......Page 739
17.1.4 Creating instances of types......Page 741
17.1.5 Defining new types with Add-Type......Page 743
17.1.6 Working with generic types......Page 753
17.2.1 Retrieving a web page......Page 754
17.2.2 Processing an RSS feed......Page 756
17.3 PowerShell and graphical user interfaces......Page 757
17.3.1 PowerShell and WinForms......Page 758
17.3.2 Creating a winforms module......Page 764
17.3.3 PowerShell and Windows Presentation Foundation......Page 767
17.4 Summary......Page 773
18 Working with COM......Page 774
18.1.1 Creating COM objects......Page 775
18.1.2 Identifying and locating COM classes......Page 776
18.2 Automating Windows with COM......Page 778
18.2.1 Exploring with the Shell.Application class......Page 779
18.2.2 Managing browser windows using COM......Page 781
18.2.3 A browser window management module......Page 784
18.3 Working with the WScript.Shell class......Page 791
18.4.1 Looking up a word using Internet Explorer......Page 793
18.4.2 Using Microsoft Word to do spell checking......Page 795
18.5 The WSH ScriptControl class......Page 797
18.5.1 Embedding VBScript code in a PowerShell script......Page 798
18.5.2 Embedding JScript code in a PowerShell script......Page 799
18.6.1 Getting started with the Schedule.Service class......Page 800
18.6.2 Listing running tasks......Page 801
18.6.3 Creating a new scheduled task......Page 802
18.6.4 Credentials and scheduled tasks......Page 803
18.6.5 Viewing the life cycle of a task......Page 806
18.7.3 Interop assemblies, wrappers, and typelibs......Page 807
18.8 Summary......Page 809
19 Management objects: WMI and WS-MAN......Page 811
19.1.1 Exploring WMI......Page 812
19.1.2 The WMI infrastructure......Page 813
19.2 The WMI cmdlets......Page 815
19.2.1 The WMI cmdlet common parameters......Page 816
19.2.2 The Get-WmiObject cmdlet......Page 818
19.2.3 The Set-WmiInstance cmdlet......Page 827
19.2.4 The Invoke-WmiMethod cmdlet......Page 833
19.2.5 The Remove-WmiObject cmdlet......Page 836
19.3 The WMI object adapter......Page 838
19.3.1 The WMI type accelerators......Page 839
19.3.2 Putting modified WMI objects back......Page 842
19.4 Exploring WS-Man......Page 844
19.4.1 The WS-Man cmdlets......Page 845
19.4.2 Using Get-WSManInstance to retrieve management data......Page 846
19.4.3 Updating resources using Set-WSManInstance......Page 854
19.4.4 Invoking methods with Invoke-WSManAction......Page 855
19.5 Summary......Page 859
20 Responding in real time with eventing......Page 861
20.1 Foundations of event handling......Page 862
20.2 Synchronous events......Page 863
20.2.2 Delegates and delegation......Page 864
20.3 Asynchronous events......Page 867
20.3.2 The eventing cmdlets......Page 868
20.4 Working with asynchronous .NET events......Page 869
20.4.1 Writing a timer event handler......Page 870
20.4.2 Managing event subscriptions......Page 873
20.5.1 Automatic variables in the event handler......Page 874
20.5.2 Dynamic modules and event handler state......Page 876
20.6 Queued events and the Wait-Event cmdlet......Page 877
20.7.1 WMI event basics......Page 880
20.7.2 Class-based WMI event registration......Page 881
20.7.3 Query-based WMI event registrations......Page 885
20.8.1 Predefined engine events......Page 889
20.8.2 Generating events in functions and scripts......Page 890
20.9 Remoting and event forwarding......Page 891
20.9.1 Handling remote EventLog events......Page 893
20.9.2 Serialization issues with remote events......Page 894
20.10 How eventing works......Page 896
20.11 Summary......Page 899
21 Security, security, security......Page 902
21.1.1 What security is and what it isn’t......Page 903
21.1.2 Security: perception and reality......Page 904
21.2.1 Introduction to threat modeling......Page 905
21.2.2 Classifying threats using the STRIDE model......Page 906
21.2.3 Security basics: threats, assets, and mitigations......Page 907
21.3.1 Secure by default......Page 911
21.3.2 Enabling scripting with execution policy......Page 912
21.4.1 How public key encryption and one-way hashing work......Page 918
21.4.3 Self-signed certificates......Page 919
21.4.4 Using a certificate to sign a script......Page 923
21.4.5 Enabling strong private key protection......Page 927
21.4.6 Using the PFX file to sign a file......Page 929
21.6 Using the SecureString class......Page 930
21.6.1 Creating a SecureString object......Page 931
21.6.2 The SecureString cmdlets......Page 932
21.6.3 Working with credentials......Page 933
21.6.4 Avoiding Invoke-Expression......Page 937
21.7 Summary......Page 940
A......Page 942
B......Page 945
C......Page 946
D......Page 951
E......Page 954
F......Page 958
G......Page 960
H......Page 962
I......Page 963
J......Page 965
L......Page 966
M......Page 967
N......Page 971
O......Page 972
P......Page 974
Q......Page 979
R......Page 980
S......Page 982
T......Page 990
V......Page 993
W......Page 995
Z......Page 997