| Preface |
|
xvii | |
| Acknowledgments |
|
xix | |
| About This Book |
|
xx | |
| About The Authors |
|
xxii | |
| Part 1: Introduction To Scripting |
|
1 | (66) |
|
|
|
3 | (5) |
|
|
|
3 | (1) |
|
1.2 Is this book for you? |
|
|
4 | (1) |
|
1.3 Here's what you need to have |
|
|
5 | (1) |
|
|
|
5 | (1) |
|
Administrative privileges |
|
|
5 | (1) |
|
|
|
5 | (1) |
|
|
|
6 | (1) |
|
|
|
6 | (1) |
|
|
|
7 | (1) |
|
|
|
7 | (1) |
|
|
|
7 | (1) |
|
2 Setting up your scripting environment |
|
|
8 | (11) |
|
|
|
8 | (1) |
|
|
|
9 | (1) |
|
2.3 Administrative privileges and execution policy |
|
|
9 | (1) |
|
|
|
9 | (4) |
|
2.5 Setting up a virtual environment |
|
|
13 | (1) |
|
|
|
14 | (1) |
|
|
|
14 | (4) |
|
|
|
18 | (1) |
|
3 WWPD: what would PowerShell do? |
|
|
19 | (6) |
|
3.1 Writing single-task tools |
|
|
19 | (1) |
|
|
|
20 | (1) |
|
|
|
21 | (1) |
|
|
|
22 | (1) |
|
|
|
23 | (1) |
|
|
|
23 | (1) |
|
|
|
24 | (1) |
|
4 Review: parameter binding and the PowerShell pipeline |
|
|
25 | (11) |
|
4.1 Visualizing the pipeline |
|
|
25 | (1) |
|
4.2 It's all in the parameters |
|
|
26 | (1) |
|
|
|
27 | (4) |
|
Introducing Trace-Command |
|
|
28 | (1) |
|
Tracing ByValue parameter binding |
|
|
28 | (3) |
|
|
|
31 | (1) |
|
|
|
31 | (4) |
|
Let's trace ByPropertyName |
|
|
32 | (2) |
|
When ByPropertyName fails |
|
|
34 | (1) |
|
|
|
35 | (1) |
|
|
|
35 | (1) |
|
5 Scripting language crash course |
|
|
36 | (12) |
|
|
|
36 | (2) |
|
|
|
37 | (1) |
|
|
|
38 | (1) |
|
Troubleshooting comparisons |
|
|
38 | (1) |
|
|
|
38 | (3) |
|
5.3 The ForEach construct |
|
|
41 | (3) |
|
|
|
44 | (1) |
|
5.5 The Do/While construct |
|
|
44 | (1) |
|
|
|
45 | (1) |
|
|
|
46 | (1) |
|
|
|
47 | (1) |
|
6 The many forms of scripting (and which to use) |
|
|
48 | (10) |
|
6.1 Tools vs. controllers |
|
|
48 | (1) |
|
|
|
49 | (2) |
|
6.3 Thinking about controllers |
|
|
51 | (1) |
|
6.4 Comparing tools and controllers |
|
|
52 | (1) |
|
6.5 Some concrete examples |
|
|
52 | (4) |
|
Emailing users whose passwords are about to expire |
|
|
53 | (1) |
|
|
|
53 | (1) |
|
|
|
54 | (1) |
|
|
|
55 | (1) |
|
|
|
56 | (1) |
|
|
|
56 | (2) |
|
|
|
58 | (9) |
|
7.1 PowerShell's script security goal |
|
|
58 | (1) |
|
|
|
59 | (4) |
|
|
|
61 | (1) |
|
|
|
62 | (1) |
|
Setting an execution policy |
|
|
62 | (1) |
|
7.3 PowerShell isn't the default application |
|
|
63 | (1) |
|
|
|
63 | (2) |
|
|
|
65 | (1) |
|
|
|
66 | (1) |
| Part 2: Building A Powershell Script |
|
67 | (102) |
|
|
|
69 | (11) |
|
|
|
69 | (2) |
|
|
|
71 | (1) |
|
|
|
72 | (1) |
|
|
|
72 | (1) |
|
|
|
73 | (4) |
|
|
|
77 | (3) |
|
|
|
77 | (1) |
|
|
|
78 | (1) |
|
|
|
78 | (2) |
|
9 Avoiding bugs: start with a command |
|
|
80 | (8) |
|
|
|
80 | (2) |
|
9.2 Breaking it down, and running it right |
|
|
82 | (1) |
|
9.3 Running commands and digging deeper |
|
|
83 | (2) |
|
|
|
85 | (1) |
|
|
|
85 | (1) |
|
|
|
85 | (3) |
|
|
|
86 | (1) |
|
|
|
86 | (1) |
|
|
|
86 | (2) |
|
10 Building a basic function and script module |
|
|
88 | (11) |
|
10.1 Starting with a basic function |
|
|
88 | (4) |
|
Designing the input parameters |
|
|
89 | (1) |
|
|
|
90 | (1) |
|
|
|
91 | (1) |
|
10.2 Creating a script module |
|
|
92 | (1) |
|
|
|
93 | (1) |
|
|
|
93 | (2) |
|
|
|
95 | (4) |
|
|
|
95 | (1) |
|
|
|
96 | (1) |
|
|
|
96 | (3) |
|
11 Going advanced with your function |
|
|
99 | (12) |
|
11.1 About CmdletBinding and common parameters |
|
|
99 | (8) |
|
|
|
101 | (3) |
|
|
|
104 | (1) |
|
|
|
104 | (1) |
|
|
|
105 | (1) |
|
Supporting-Confirm and-WhatIf |
|
|
106 | (1) |
|
|
|
107 | (4) |
|
|
|
108 | (1) |
|
|
|
108 | (1) |
|
|
|
108 | (3) |
|
12 Objects: the best kind of output |
|
|
111 | (11) |
|
12.1 Assembling the information |
|
|
112 | (1) |
|
12.2 Constructing and emitting output |
|
|
113 | (1) |
|
|
|
114 | (2) |
|
12.4 An object alternative |
|
|
116 | (1) |
|
|
|
117 | (1) |
|
|
|
118 | (4) |
|
|
|
118 | (1) |
|
|
|
119 | (1) |
|
|
|
120 | (2) |
|
13 Using all the pipelines |
|
|
122 | (14) |
|
13.1 Knowing the six channels |
|
|
122 | (1) |
|
13.2 Adding verbose and warning output |
|
|
123 | (2) |
|
13.3 Doing more with-Verbose |
|
|
125 | (2) |
|
|
|
127 | (5) |
|
A detailed information example |
|
|
129 | (3) |
|
|
|
132 | (4) |
|
|
|
132 | (2) |
|
|
|
134 | (1) |
|
|
|
134 | (2) |
|
14 Simple help: making a comment |
|
|
136 | (10) |
|
14.1 Where to put your help |
|
|
136 | (1) |
|
|
|
137 | (3) |
|
14.3 Going further with comment-based help |
|
|
140 | (1) |
|
|
|
140 | (1) |
|
|
|
140 | (1) |
|
|
|
141 | (5) |
|
|
|
141 | (2) |
|
|
|
143 | (1) |
|
|
|
143 | (3) |
|
|
|
146 | (12) |
|
15.1 Understanding errors and exceptions |
|
|
146 | (1) |
|
|
|
147 | (1) |
|
15.3 Two reasons for exception handling |
|
|
148 | (1) |
|
15.4 Handling exceptions in your tool |
|
|
148 | (3) |
|
15.5 Capturing the exception |
|
|
151 | (1) |
|
15.6 Handling exceptions for non-commands |
|
|
151 | (1) |
|
15.7 Going further with exception handling |
|
|
152 | (1) |
|
|
|
153 | (5) |
|
|
|
153 | (1) |
|
|
|
154 | (1) |
|
|
|
155 | (3) |
|
16 Filling out a manifest |
|
|
158 | (11) |
|
16.1 Module execution order |
|
|
158 | (1) |
|
16.2 Creating a new manifest |
|
|
159 | (3) |
|
16.3 Examining the manifest |
|
|
162 | (2) |
|
|
|
162 | (1) |
|
|
|
162 | (1) |
|
|
|
162 | (1) |
|
Scripts, types, and formats |
|
|
163 | (1) |
|
|
|
163 | (1) |
|
|
|
164 | (7) |
|
|
|
164 | (1) |
|
|
|
165 | (1) |
|
|
|
166 | (3) |
| Part 3: Grown-Up Scripting |
|
169 | (84) |
|
17 Changing your brain when it comes to scripting |
|
|
171 | (19) |
|
|
|
171 | (4) |
|
|
|
172 | (1) |
|
|
|
173 | (2) |
|
Thinking beyond the literal |
|
|
175 | (1) |
|
|
|
175 | (13) |
|
|
|
180 | (2) |
|
|
|
182 | (6) |
|
|
|
188 | (2) |
|
|
|
188 | (1) |
|
|
|
189 | (1) |
|
|
|
189 | (1) |
|
18 Professional-grade scripting |
|
|
190 | (12) |
|
18.1 Using source control |
|
|
190 | (1) |
|
|
|
191 | (1) |
|
18.3 Commenting your code |
|
|
192 | (1) |
|
18.4 Formatting your code |
|
|
193 | (3) |
|
18.5 Using meaningful non-Hungarian variable names |
|
|
196 | (1) |
|
|
|
196 | (1) |
|
18.7 Avoiding awkward pipelines |
|
|
197 | (1) |
|
|
|
197 | (1) |
|
18.9 Avoiding Write-Host and Read-Host |
|
|
197 | (1) |
|
18.10 Sticking with single quotes |
|
|
198 | (1) |
|
18.11 Not polluting the global scope |
|
|
198 | (1) |
|
|
|
199 | (1) |
|
|
|
199 | (1) |
|
18.14 Striving for elegance |
|
|
200 | (1) |
|
|
|
201 | (1) |
|
19 An introduction to source control with git |
|
|
202 | (19) |
|
|
|
202 | (1) |
|
|
|
203 | (1) |
|
|
|
203 | (1) |
|
|
|
204 | (1) |
|
|
|
204 | (7) |
|
|
|
205 | (1) |
|
|
|
205 | (1) |
|
|
|
206 | (1) |
|
|
|
207 | (2) |
|
|
|
209 | (2) |
|
19.4 Using git with VS Code |
|
|
211 | (4) |
|
19.5 Integrating with GitHub |
|
|
215 | (4) |
|
|
|
219 | (2) |
|
|
|
221 | (13) |
|
|
|
221 | (1) |
|
20.2 Problems with manual testing |
|
|
222 | (1) |
|
20.3 Benefits of automated testing |
|
|
222 | (1) |
|
|
|
222 | (1) |
|
|
|
223 | (1) |
|
|
|
223 | (1) |
|
|
|
223 | (1) |
|
|
|
224 | (1) |
|
Don't test what isn't yours |
|
|
224 | (1) |
|
20.7 Writing a basic Pester test |
|
|
224 | (8) |
|
|
|
226 | (1) |
|
|
|
227 | (1) |
|
|
|
227 | (1) |
|
|
|
228 | (2) |
|
|
|
230 | (2) |
|
|
|
232 | (2) |
|
|
|
234 | (10) |
|
21.1 Why sign your scripts? |
|
|
234 | (1) |
|
21.2 A word about certificates |
|
|
235 | (1) |
|
|
|
236 | (1) |
|
|
|
237 | (6) |
|
Getting a code-signing certificate |
|
|
237 | (2) |
|
Trusting self-signed certificates |
|
|
239 | (1) |
|
|
|
240 | (2) |
|
Testing script signatures |
|
|
242 | (1) |
|
|
|
243 | (1) |
|
22 Publishing your script |
|
|
244 | (9) |
|
|
|
244 | (1) |
|
22.2 Meet the PowerShell Gallery |
|
|
244 | (1) |
|
22.3 Other publishing targets |
|
|
245 | (1) |
|
|
|
245 | (1) |
|
Are you reinventing the wheel? |
|
|
245 | (1) |
|
|
|
245 | (1) |
|
|
|
246 | (1) |
|
|
|
246 | (2) |
|
|
|
247 | (1) |
|
|
|
248 | (3) |
|
Using the Microsoft script repository |
|
|
248 | (1) |
|
|
|
249 | (2) |
|
|
|
251 | (1) |
|
Managing published scripts |
|
|
251 | (1) |
|
|
|
251 | (2) |
| Part 4: Advanced Techniques |
|
253 | (65) |
|
|
|
255 | (17) |
|
23.1 The three kinds of bugs |
|
|
255 | (1) |
|
23.2 Dealing with syntax bugs |
|
|
256 | (1) |
|
23.3 Dealing with results bugs |
|
|
257 | (1) |
|
23.4 Dealing with logic bugs |
|
|
258 | (9) |
|
|
|
259 | (5) |
|
|
|
264 | (1) |
|
|
|
264 | (2) |
|
|
|
266 | (1) |
|
|
|
267 | (5) |
|
|
|
267 | (2) |
|
|
|
269 | (1) |
|
|
|
269 | (3) |
|
24 Making script output prettier |
|
|
272 | (20) |
|
|
|
272 | (1) |
|
24.2 Creating a default view |
|
|
273 | (12) |
|
Exploring Microsoft's views |
|
|
273 | (3) |
|
Adding a custom type name to output objects |
|
|
276 | (1) |
|
|
|
277 | (5) |
|
Adding the view file to a module |
|
|
282 | (3) |
|
|
|
285 | (7) |
|
|
|
285 | (1) |
|
|
|
286 | (1) |
|
|
|
286 | (6) |
|
25 Wrapping up the .NET Framework |
|
|
292 | (10) |
|
25.1 Why does PowerShell exist? |
|
|
292 | (1) |
|
25.2 A crash course in .NET |
|
|
293 | (1) |
|
|
|
294 | (2) |
|
|
|
296 | (3) |
|
25.5 A more practical example |
|
|
299 | (1) |
|
|
|
300 | (2) |
|
|
|
300 | (1) |
|
|
|
300 | (1) |
|
|
|
300 | (2) |
|
26 Storing data-not in Excel! |
|
|
302 | (12) |
|
26.1 Introducing SQL Server! |
|
|
302 | (1) |
|
26.2 Setting up everything |
|
|
303 | (2) |
|
26.3 Using your database: creating a table |
|
|
305 | (3) |
|
26.4 Saving data to SQL Server |
|
|
308 | (3) |
|
26.5 Querying data from SQL Server |
|
|
311 | (2) |
|
|
|
313 | (1) |
|
|
|
314 | (4) |
|
27.1 Welcome to toolmaking |
|
|
314 | (1) |
|
27.2 Taking your next step |
|
|
315 | (1) |
|
27.3 What's in your future? |
|
|
316 | (2) |
| Index |
|
318 | |