Foreword |
|
xvii | |
Preface |
|
xx | |
Acknowledgments |
|
xxii | |
About This Book |
|
xxiii | |
About The Authors |
|
xxvi | |
1 Before You Begin |
|
1 | (7) |
|
1.1 Why you can no longer afford to ignore PowerShell |
|
|
1 | (2) |
|
|
2 | (1) |
|
|
2 | (1) |
|
1.2 Windows, Linux, and macOS, oh my |
|
|
3 | (1) |
|
1.3 Is this book for you? |
|
|
3 | (1) |
|
|
4 | (2) |
|
|
4 | (1) |
|
|
5 | (1) |
|
|
5 | (1) |
|
|
5 | (1) |
|
|
5 | (1) |
|
1.5 Setting up your lab environment |
|
|
6 | (1) |
|
1.6 Installing PowerShell |
|
|
6 | (1) |
|
|
7 | (1) |
|
1.8 Being immediately effective with PowerShell |
|
|
7 | (1) |
2 Meet PowerShell |
|
8 | (17) |
|
2.1 PowerShell on Windows |
|
|
9 | (3) |
|
|
12 | (1) |
|
|
12 | (1) |
|
2.3 PowerShell on Linux (Ubuntu 18.04) |
|
|
13 | (1) |
|
Installation on Ubuntu 18.04 |
|
|
14 | (1) |
|
2.4 Visual Studio Code and the PowerShell extension |
|
|
14 | (7) |
|
Installing Visual Studio Code and the PowerShell extension |
|
|
15 | (1) |
|
Getting familiar with Visual Studio Code |
|
|
16 | (3) |
|
Customizing Visual Studio Code and the PowerShell extension |
|
|
19 | (2) |
|
2.5 It's typing class all over again |
|
|
21 | (2) |
|
2.6 What version is this? |
|
|
23 | (1) |
|
|
23 | (2) |
3 Using the help system |
|
25 | (17) |
|
3.1 The help system: How you discover commands |
|
|
25 | (2) |
|
|
27 | (1) |
|
|
28 | (1) |
|
3.4 Using help to find commands |
|
|
29 | (2) |
|
3.5 Interpreting the help |
|
|
31 | (7) |
|
Parameter sets and common parameters |
|
|
31 | (1) |
|
Optional and mandatory parameters |
|
|
32 | (1) |
|
|
33 | (2) |
|
|
35 | (2) |
|
|
37 | (1) |
|
3.6 Accessing "about" topics |
|
|
38 | (1) |
|
3.7 Accessing online help |
|
|
38 | (1) |
|
|
39 | (1) |
|
|
40 | (2) |
4 Running commands |
|
42 | (16) |
|
|
42 | (4) |
|
|
43 | (3) |
|
4.2 Not scripting, but running commands |
|
|
46 | (1) |
|
4.3 The anatomy of a command |
|
|
47 | (1) |
|
4.4 The cmdlet naming convention |
|
|
48 | (1) |
|
4.5 Aliases: Nicknames for commands |
|
|
49 | (1) |
|
|
50 | (2) |
|
Truncating parameter names |
|
|
50 | (1) |
|
Using parameter name aliases |
|
|
50 | (1) |
|
Using positional parameters |
|
|
51 | (1) |
|
4.7 Support for external commands |
|
|
52 | (2) |
|
|
54 | (1) |
|
4.9 Common points of confusion |
|
|
55 | (1) |
|
|
55 | (1) |
|
|
56 | (1) |
|
|
56 | (1) |
|
|
57 | (1) |
5 Working with providers |
|
58 | (11) |
|
|
58 | (2) |
|
5.2 Understanding how the filesystem is organized |
|
|
60 | (2) |
|
5.3 Navigating the filesystem |
|
|
62 | (1) |
|
5.4 Using wildcards and literal paths |
|
|
63 | (1) |
|
5.5 Working with other providers |
|
|
64 | (4) |
|
|
65 | (3) |
|
|
68 | (1) |
|
|
68 | (1) |
6 The pipeline: Connecting commands |
|
69 | (15) |
|
6.1 Connecting one command to another: Less work for you |
|
|
69 | (1) |
|
|
70 | (7) |
|
|
71 | (1) |
|
|
72 | (2) |
|
|
74 | (1) |
|
|
74 | (1) |
|
|
75 | (2) |
|
|
77 | (1) |
|
|
78 | (1) |
|
6.5 Using cmdlets that modify the system: Killing processes |
|
|
79 | (1) |
|
6.6 Common points of confusion |
|
|
80 | (2) |
|
|
82 | (1) |
|
|
82 | (2) |
7 Adding commands |
|
84 | (10) |
|
7.1 How one shell can do everything |
|
|
84 | (1) |
|
7.2 Extensions: Finding and installing modules |
|
|
84 | (3) |
|
7.3 Extensions: Finding and adding modules |
|
|
87 | (3) |
|
7.4 Command conflicts and removing extensions |
|
|
90 | (1) |
|
7.5 Playing with a new module |
|
|
91 | (1) |
|
7.6 Common points of confusion |
|
|
92 | (1) |
|
|
92 | (1) |
|
|
93 | (1) |
8 Objects: Data by another name |
|
94 | (12) |
|
|
94 | (1) |
|
8.2 Understanding why PowerShell uses objects |
|
|
95 | (2) |
|
8.3 Discovering objects: Get-Member |
|
|
97 | (2) |
|
8.4 Using object attributes, or properties |
|
|
99 | (1) |
|
8.5 Using object actions, or methods |
|
|
100 | (1) |
|
|
100 | (1) |
|
8.7 Selecting the properties you want |
|
|
101 | (1) |
|
8.8 Objects until the end |
|
|
102 | (2) |
|
8.9 Common points of confusion |
|
|
104 | (1) |
|
|
104 | (1) |
|
|
105 | (1) |
9 A practical interlude |
|
106 | (8) |
|
|
106 | (1) |
|
|
106 | (2) |
|
9.3 Learning to use the commands |
|
|
108 | (4) |
|
9.4 Tips for teaching yourself |
|
|
112 | (1) |
|
|
112 | (1) |
|
|
113 | (1) |
10 The pipeline, deeper |
|
114 | (18) |
|
10.1 The pipeline: Enabling power with less typing |
|
|
114 | (1) |
|
10.2 How PowerShell passes data down the pipeline |
|
|
114 | (2) |
|
10.3 Plan A: Pipeline input ByValue |
|
|
116 | (3) |
|
10.4 Plan B: Pipeline input ByPropertyName |
|
|
119 | (4) |
|
10.5 When things don't line up: Custom properties |
|
|
123 | (3) |
|
10.6 Working with Azure PowerShell |
|
|
126 | (1) |
|
10.7 Parenthetical commands |
|
|
126 | (1) |
|
10.8 Extracting the value from a single property |
|
|
127 | (2) |
|
|
129 | (2) |
|
|
131 | (1) |
|
10.11 Further exploration |
|
|
131 | (1) |
11 Formatting: And why it's done on the right |
|
132 | (17) |
|
11.1 Formatting: Making what you see prettier |
|
|
132 | (1) |
|
11.2 Working with the default formatting |
|
|
132 | (5) |
|
|
137 | (2) |
|
|
139 | (1) |
|
11.5 Formatting wide lists |
|
|
140 | (1) |
|
11.6 Creating custom columns and list entries |
|
|
141 | (2) |
|
11.7 Going out: To a file or to the host |
|
|
143 | (1) |
|
11.8 Another out: GridViews |
|
|
144 | (1) |
|
11.9 Common points of confusion |
|
|
144 | (4) |
|
|
145 | (1) |
|
One type of object at a time, please |
|
|
146 | (2) |
|
|
148 | (1) |
|
|
148 | (1) |
|
11.12 Further exploration |
|
|
148 | (1) |
12 Filtering and comparisons |
|
149 | (8) |
|
12.1 Making the shell give you just what you need |
|
|
149 | (1) |
|
|
150 | (1) |
|
12.3 Using comparison operators |
|
|
150 | (2) |
|
12.4 Filtering objects out of the pipeline |
|
|
152 | (1) |
|
12.5 Using the iterative command-line model |
|
|
153 | (2) |
|
12.6 Common points of confusion |
|
|
155 | (1) |
|
|
155 | (1) |
|
|
155 | (1) |
|
|
156 | (1) |
|
|
156 | (1) |
|
|
156 | (1) |
13 Remote control: One-to-one and one-to-many |
|
157 | (17) |
|
13.1 The idea behind remote PowerShell |
|
|
157 | (2) |
|
Remoting on Windows devices |
|
|
158 | (1) |
|
Remoting on macOS and Linux devices |
|
|
158 | (1) |
|
|
158 | (1) |
|
13.2 Setting up PSRP over SSH |
|
|
159 | (2) |
|
|
159 | (1) |
|
Setting up SSH on Windows |
|
|
160 | (1) |
|
13.3 PSRP over SSH overview |
|
|
161 | (1) |
|
|
162 | (2) |
|
13.5 Using Enter-PSSession and Exit-PSSession for one-to-one remoting |
|
|
164 | (3) |
|
13.6 Using Invoke-ScriptBlock for one-to-many remoting |
|
|
167 | (1) |
|
13.7 Differences between remote and local commands |
|
|
168 | (4) |
|
|
168 | (2) |
|
Local vs. remote processing |
|
|
170 | (2) |
|
13.8 But wait, there's more |
|
|
172 | (1) |
|
13.9 Common points of confusion |
|
|
172 | (1) |
|
|
172 | (1) |
|
|
173 | (1) |
|
13.12 Further exploration |
|
|
173 | (1) |
14 Multitasking with background jobs |
|
174 | (13) |
|
14.1 Making PowerShell do multiple things at the same time |
|
|
174 | (1) |
|
14.2 Synchronous vs. asynchronous |
|
|
175 | (1) |
|
14.3 Creating a process job |
|
|
175 | (1) |
|
14.4 Creating a thread job |
|
|
176 | (1) |
|
|
177 | (1) |
|
|
177 | (2) |
|
|
179 | (3) |
|
14.8 Working with child jobs |
|
|
182 | (1) |
|
14.9 Commands for managing jobs |
|
|
183 | (2) |
|
14.10 Common points of confusion |
|
|
185 | (1) |
|
|
186 | (1) |
|
|
186 | (1) |
15 Working with many objects, one at a time |
|
187 | (15) |
|
15.1 The preferred way: "Batch" cmdlets |
|
|
187 | (1) |
|
15.2 The CIM way: Invoking methods |
|
|
188 | (4) |
|
15.3 The backup plan: Enumerating objects |
|
|
192 | (2) |
|
Making the cmdlets work for you |
|
|
193 | (1) |
|
15.4 Let's speed things up |
|
|
194 | (3) |
|
15.5 Common points of confusion |
|
|
197 | (3) |
|
Which way is the right way? |
|
|
197 | (1) |
|
Diminishing returns of Parallel ForEach |
|
|
198 | (1) |
|
|
199 | (1) |
|
|
200 | (1) |
|
|
200 | (1) |
|
|
200 | (2) |
16 Variables: A place to store your stuff |
|
202 | (16) |
|
16.1 Introduction to variables |
|
|
202 | (1) |
|
16.2 Storing values in variables |
|
|
202 | (3) |
|
16.3 Using variables: Fun tricks with quotes |
|
|
205 | (2) |
|
16.4 Storing many objects in a variable |
|
|
207 | (4) |
|
Working with single objects in a variable |
|
|
207 | (2) |
|
Working with multiple objects in a variable |
|
|
209 | (1) |
|
Other ways to work with multiple objects |
|
|
209 | (1) |
|
Unrolling properties and methods in PowerShell |
|
|
210 | (1) |
|
16.5 More tricks with double quotes |
|
|
211 | (1) |
|
16.6 Declaring a variable's type |
|
|
212 | (3) |
|
16.7 Commands for working with variables |
|
|
215 | (1) |
|
16.8 Variable best practices |
|
|
215 | (1) |
|
16.9 Common points of confusion |
|
|
216 | (1) |
|
|
216 | (1) |
|
|
216 | (1) |
|
16.12 Further exploration |
|
|
217 | (1) |
17 Input and output |
|
218 | (8) |
|
17.1 Prompting for, and displaying, information |
|
|
218 | (1) |
|
|
219 | (1) |
|
|
220 | (2) |
|
|
222 | (1) |
|
|
223 | (2) |
|
|
225 | (1) |
|
|
225 | (1) |
|
|
225 | (1) |
18 Sessions: Remote control with less work |
|
226 | (10) |
|
18.1 Creating and using reusable sessions |
|
|
227 | (2) |
|
18.2 Enter-PSSession with session objects |
|
|
229 | (2) |
|
18.3 Invoke-Command with session objects |
|
|
231 | (1) |
|
18.4 Implicit remoting: Importing a session |
|
|
231 | (2) |
|
18.5 Using disconnected sessions |
|
|
233 | (1) |
|
|
234 | (1) |
|
|
235 | (1) |
|
|
235 | (1) |
19 You call this scripting? |
|
236 | (13) |
|
19.1 Not programming, more like batch files |
|
|
236 | (1) |
|
19.2 Making commands repeatable |
|
|
237 | (1) |
|
19.3 Parameterizing commands |
|
|
238 | (2) |
|
19.4 Creating a parameterized script |
|
|
240 | (1) |
|
19.5 Documenting your script |
|
|
241 | (2) |
|
19.6 One script, one pipeline |
|
|
243 | (2) |
|
19.7 A quick look at scope |
|
|
245 | (2) |
|
|
247 | (1) |
|
|
247 | (2) |
20 Improving your parameterized script |
|
249 | (10) |
|
|
249 | (1) |
|
20.2 Getting PowerShell to do the hard work |
|
|
250 | (1) |
|
20.3 Making parameters mandatory |
|
|
251 | (2) |
|
20.4 Adding parameter aliases |
|
|
253 | (1) |
|
20.5 Validating parameter input |
|
|
254 | (1) |
|
20.6 Adding the warm and fuzzies with verbose output |
|
|
255 | (2) |
|
|
257 | (1) |
|
|
257 | (2) |
21 Using regular expressions to parse text files |
|
259 | (7) |
|
21.1 The purpose of regular expressions |
|
|
259 | (1) |
|
21.2 A regex syntax primer |
|
|
260 | (2) |
|
21.3 Using regex with -Match |
|
|
262 | (1) |
|
21.4 Using regex with Select-String |
|
|
262 | (2) |
|
|
264 | (1) |
|
|
265 | (1) |
|
|
265 | (1) |
22 Using someone else's script |
|
266 | (7) |
|
|
266 | (3) |
|
|
268 | (1) |
|
|
268 | (1) |
|
22.2 It's a line-by-line examination |
|
|
269 | (1) |
|
|
269 | (2) |
|
|
271 | (2) |
23 Adding logic and loops |
|
273 | (6) |
|
23.1 Foreach and Foreach-Object |
|
|
273 | (4) |
|
|
273 | (1) |
|
|
274 | (1) |
|
|
275 | (2) |
|
|
277 | (1) |
|
|
278 | (1) |
|
|
278 | (1) |
|
|
278 | (1) |
24 Handling errors |
|
279 | (8) |
|
24.1 Understanding errors and exceptions |
|
|
280 | (1) |
|
|
281 | (1) |
|
24.3 Two reasons for exception handling |
|
|
282 | (1) |
|
|
282 | (2) |
|
24.5 Handling exceptions for noncommands |
|
|
284 | (1) |
|
24.6 Going further with exception handling |
|
|
284 | (1) |
|
|
285 | (1) |
|
|
285 | (2) |
25 Debugging techniques |
|
287 | (9) |
|
|
287 | (2) |
|
|
289 | (4) |
|
25.3 Hey, script, stop right there...with breakpoints |
|
|
293 | (2) |
|
|
295 | (1) |
26 Tips, tricks, and techniques |
|
296 | (14) |
|
26.1 Profiles, prompts, and colors: Customizing the shell |
|
|
296 | (4) |
|
|
296 | (2) |
|
|
298 | (1) |
|
|
299 | (1) |
|
26.2 Operators: -as, -is, -replace, -join, -split, -contains, -in |
|
|
300 | (3) |
|
|
300 | (1) |
|
|
301 | (1) |
|
|
301 | (1) |
|
|
302 | (1) |
|
|
303 | (1) |
|
|
304 | (2) |
|
26.5 Dealing with WMI dates |
|
|
306 | (1) |
|
26.6 Setting default parameter values |
|
|
307 | (1) |
|
26.7 Playing with script blocks |
|
|
308 | (1) |
|
26.8 More tips, tricks, and techniques |
|
|
309 | (1) |
27 Never the end |
|
310 | (3) |
|
27.1 Ideas for further exploration |
|
|
310 | (1) |
|
27.2 "Now that I've read the book, where do I start?" |
|
|
311 | (1) |
|
27.3 Other resources you'll grow to love |
|
|
312 | (1) |
Appendix PowerShell cheat sheet |
|
313 | (8) |
Index |
|
321 | |