Mastering Windows PowerShell scripting one-stop guide to automating administrative tasks [Second edition] 9781787126305, 1787126307, 9781787121713, 1787121712

Master the art of automating and managing your environment using PowerShell About This Book Find quick solutions to auto

4,569 506 2MB

English Pages 440 [428] Year 2017

Report DMCA / Copyright

DOWNLOAD FILE

Mastering Windows PowerShell scripting one-stop guide to automating administrative tasks [Second edition]
 9781787126305, 1787126307, 9781787121713, 1787121712

Table of contents :
Cover......Page 1
Copyright......Page 3
Credits......Page 4
About the Authors......Page 5
About the Reviewer......Page 6
www.PacktPub.com......Page 7
Customer Feedback......Page 8
Table of Contents......Page 9
Preface......Page 23
Chapter 1: Introduction to PowerShell......Page 28
Tick in PowerShell......Page 29
Creating arrays and hashtables......Page 31
Strings......Page 32
Common reserved variables......Page 33
PowerShell editors......Page 34
PowerShell ISE......Page 35
Installing ISE Preview......Page 36
Features......Page 37
Console......Page 38
Version control (Git)......Page 40
PowerShell on Linux......Page 41
Where are the PowerShell files?......Page 42
Profiles......Page 43
Multiplatform scripting......Page 44
File encoding......Page 45
Example......Page 46
Summary......Page 47
Getting help......Page 48
Updatable help......Page 49
The Get-Help command......Page 50
Examples......Page 51
Detailed and Full switches......Page 52
Save-Help......Page 53
Update-Help......Page 54
About help files......Page 55
Verbs......Page 56
Finding commands......Page 57
Aliases......Page 58
Optional parameters......Page 59
Mandatory positional parameters......Page 60
Common parameters......Page 61
Parameter values......Page 62
Parameter sets......Page 63
Confirm parameter......Page 64
ConfirmPreference......Page 65
WhatIfPreference......Page 67
Providers......Page 68
Drives using providers......Page 69
Using providers......Page 71
Summary......Page 73
Chapter 3: Modules and Snap-Ins......Page 74
What is a module?......Page 75
What is the PowerShell Gallery?......Page 76
The Get-Module command......Page 78
The Import-Module command......Page 79
The Remove-Module command......Page 80
The Install-Module command......Page 81
What is a snap-in?......Page 82
Using snap-ins......Page 83
Summary......Page 84
Chapter 4: Working with Objects in PowerShell......Page 85
Non-standard output......Page 86
Members......Page 87
The Get-Member command......Page 88
Accessing properties......Page 89
Using methods......Page 90
Access modifiers......Page 92
The ForEach-Object command......Page 94
Selecting and sorting......Page 95
The Select-Object command......Page 96
The Sort-Object command......Page 99
The Group-Object command......Page 101
The Measure-Object command......Page 104
Comparing......Page 105
The Export-Csv command......Page 107
The Import-Csv command......Page 109
Export-Clixml and Import-Clixml......Page 110
Summary......Page 111
Arithmetic operators......Page 112
Addition and subtraction operators......Page 113
Multiplication, division, and modulus operators......Page 115
Shift left and shift right operators......Page 116
Assign, add and assign, and subtract and assign......Page 119
Multiply and assign, divide and assign, and modulus and assign......Page 121
Comparison operators......Page 122
Comparison operators and arrays......Page 123
Equal to and not equal to......Page 124
Greater than and less than......Page 125
Regular-expression-based operators......Page 126
Match and not match......Page 127
Replace......Page 128
Binary operators......Page 129
Binary or......Page 130
Binary not......Page 131
Or......Page 132
Not......Page 133
Is and isnot......Page 134
Redirection operators......Page 135
Redirection to a file......Page 136
Redirecting streams to standard output......Page 137
Redirection to null......Page 138
Comma......Page 139
Increment and decrement......Page 140
Join......Page 141
Summary......Page 142
Chapter 6: Variables, Arrays, and Hashtables......Page 143
Naming and creating variables......Page 144
Clear-Variable......Page 145
New-Variable......Page 146
Set-Variable......Page 147
Local and Global scope......Page 148
Private scope......Page 149
Script scope......Page 150
Type and type conversion......Page 151
Objects assigned to variables......Page 153
Arrays......Page 154
Creating an array......Page 155
Adding elements to an array......Page 156
Selecting elements from an array......Page 157
Removing elements from an array......Page 158
Removing elements by index......Page 159
Clearing an array......Page 160
Multi-dimensional and jagged arrays......Page 161
Hashtables......Page 162
Adding and changing elements to a hashtable......Page 163
Selecting elements from a hashtable......Page 165
Removing elements from a hashtable......Page 166
Lists, dictionaries, queues, and stacks......Page 167
Adding elements to the list......Page 168
Selecting elements from the list......Page 169
Changing element values in a list......Page 170
Adding and changing elements in a dictionary......Page 171
Selecting elements from a dictionary......Page 172
Queues......Page 173
Removing elements from the queue......Page 174
Creating a stack......Page 175
Removing elements from the stack......Page 176
Summary......Page 177
Conditional statements......Page 178
If, else, and elseif......Page 179
Assignment within if statements......Page 180
Switch......Page 181
Wildcard and Regex......Page 182
Foreach......Page 183
Do until and do while......Page 184
While......Page 185
Break and continue......Page 186
Summary......Page 187
Chapter 8: Working with .NET......Page 188
Assemblies......Page 189
Types......Page 190
Constructors......Page 191
Calling constructors with lists of arguments......Page 193
Arguments as an array......Page 195
Properties and methods......Page 196
Static properties......Page 198
Static methods......Page 200
Non-public classes......Page 202
Type accelerators......Page 203
Using namespaces......Page 204
Summary......Page 206
Indexing into strings......Page 207
String methods and arrays......Page 208
Split......Page 209
Trim, TrimStart, and TrimEnd......Page 211
Insert and Remove......Page 212
IndexOf and LastIndexOf......Page 213
ToUpper, ToLower, and ToTitleCase......Page 214
Contains, StartsWith, and EndsWith......Page 215
Working with Base64......Page 216
How Base64 works......Page 218
Working with CSV......Page 220
Convert-String......Page 221
ConvertFrom-String......Page 222
Large byte values......Page 223
Using System.Math......Page 224
Date and time manipulation......Page 225
Parsing dates......Page 226
Changing dates......Page 228
Comparing dates......Page 229
Summary......Page 230
Chapter 10: Regular Expressions......Page 231
Debugging regular expressions......Page 232
Literal characters......Page 233
Any character (.)......Page 234
The escape character (\)......Page 235
Non-printable characters......Page 236
Anchors......Page 237
Repetition......Page 238
Exploring the quantifiers......Page 239
Ranges......Page 240
Character class subtraction......Page 242
Alternation......Page 243
Repeating groups......Page 244
Restricting alternation......Page 245
Capturing values......Page 246
Named capture groups......Page 247
Non-capturing groups......Page 249
MAC addresses......Page 250
IP addresses......Page 251
Netstat command......Page 252
Summary......Page 254
Working with providers......Page 255
Getting items......Page 256
Drives......Page 257
Testing existence......Page 258
Creating and deleting items......Page 259
Filesystem properties......Page 260
Adding and removing file attributes......Page 261
Registry values......Page 262
Permissions......Page 263
Ownership......Page 264
Rule protection......Page 265
Inheritance and propagation flags......Page 268
Removing access control entries......Page 269
Copying lists and entries......Page 270
Adding access control entries......Page 271
Filesystem rights......Page 272
Registry rights......Page 273
Transactions......Page 274
New-FileCatalog......Page 275
Test-FileCatalog......Page 276
Summary......Page 277
Working with WMI......Page 278
WMI commands......Page 279
Understanding SELECT, WHERE, and FROM......Page 280
Escape sequences and wildcard characters......Page 281
Comparison operators......Page 282
Associated classes......Page 283
WMI object path......Page 284
Using ASSOCIATORS OF......Page 285
CIM cmdlets......Page 286
Getting classes......Page 287
Calling methods......Page 288
Creating instances......Page 290
Working with CIM sessions......Page 291
Associated classes......Page 293
Getting instances......Page 294
Calling methods......Page 295
Sharing permissions......Page 298
Getting a security descriptor......Page 299
Adding an access control entry......Page 301
WMI permissions......Page 302
WMI and SDDL......Page 303
Summary......Page 305
ConvertTo-Html......Page 306
Adding style......Page 307
HTML and special characters......Page 308
Elements and attributes......Page 309
Schemas......Page 310
ConvertTo-Xml......Page 311
XML type accelerator......Page 312
XPath and Select-Xml......Page 313
Working with namespaces......Page 314
Creating documents......Page 315
Modifying element and attribute values......Page 316
Adding elements......Page 317
Copying nodes between documents......Page 318
Schema validation......Page 319
System.Xml.Linq......Page 320
Selecting nodes......Page 321
Creating documents......Page 322
Working with namespaces......Page 323
Modifying element and attribute values......Page 324
Removing nodes......Page 325
Schema validation......Page 326
ConvertTo-Json......Page 327
ConvertFrom-Json......Page 328
Summary......Page 330
Web requests......Page 331
HTTPS......Page 332
Bypassing SSL errors......Page 333
Capturing SSL errors......Page 334
Simple requests......Page 336
Requests with arguments......Page 337
Walking through OAuth......Page 338
Creating an application......Page 339
Getting an authorization code......Page 340
Requesting an access token......Page 341
Getting a list of tracks......Page 342
New-WebServiceProxy......Page 343
Methods......Page 344
Types......Page 345
Namespaces......Page 347
Summary......Page 348
WS-Management......Page 349
Get-WSManInstance......Page 350
Remoting and SSL......Page 351
Set-WSManQuickConfig......Page 352
Remoting permissions GUI......Page 353
Remoting permissions by script......Page 355
User Account Control......Page 357
Trusted hosts......Page 358
New-CimSession......Page 359
PS sessions......Page 360
Invoke-Command......Page 361
Local functions and remote sessions......Page 362
Using splatting with ArgumentList......Page 363
Disconnected sessions......Page 364
The using variable scope......Page 365
Export-PSSession......Page 366
The double-hop problem......Page 367
CredSSP......Page 368
Summary......Page 369
Static analysis......Page 370
Abstract syntax tree......Page 371
PSScriptAnalyzer......Page 372
Suppressing rules......Page 373
Why write tests?......Page 374
Describe and It......Page 375
Test cases......Page 377
Assertions......Page 379
BeNullOrEmpty......Page 380
Exist......Page 381
Throw......Page 382
Context......Page 383
Before and After......Page 384
TestDrive......Page 386
Assert-MockCalled......Page 387
Parameter filtering......Page 388
Fabricating objects......Page 389
CIM objects......Page 390
Pester in practice......Page 391
Summary......Page 394
Error types......Page 395
Non-terminating errors......Page 396
Error action......Page 397
Error records......Page 398
Write-Error......Page 400
Throw and ThrowTerminatingError......Page 401
Error and ErrorVariable......Page 402
Try, catch, and finally......Page 403
Catch......Page 404
Rethrowing errors......Page 406
Inconsistent error behavior......Page 408
Throw and ErrorAction......Page 410
Nesting try-catch-finally......Page 413
Terminating or non-terminating......Page 414
Trap......Page 415
Trap, scope, and continue......Page 416
Summary......Page 417
Index......Page 418

Polecaj historie