Read a script before running it, then run it with -WhatIf
Task
Write a short reporting script, then practise the skill that matters more: reading a script written by someone else and deciding whether it should run. The reading checklist is the deliverable, because a support technician is asked to make that judgement far more often than to write anything.
Steps
- Write a script at
lab/ps/report.ps1that outputs the ten processes using the most memory, with their names and memory figures, and run it. Save its output tolab/ps/report.txt. - Record the current execution policy in
lab/ps/policy.txtalong with one sentence on why RemoteSigned is the usual working answer and why policy is not a security boundary. - Write
lab/ps/checklist.mdgiving at least six things to look for when reading an unfamiliar script, each with why it matters. - Write a second script at
lab/ps/risky.ps1that would delete files in a directory you create, add-WhatIfsupport to it, and run it with-WhatIf. Save the output tolab/ps/whatif.txt-- nothing is deleted. - Write
lab/ps/extensions.csvwith headerextension,interpreter,typical_riskcovering at least five script file types.
Verify
test -s lab/ps/report.ps1 && grep -Eic 'get-process' lab/ps/report.ps1
grep -c . lab/ps/report.txt
grep -Eic 'restricted|remotesigned|allsigned|bypass' lab/ps/policy.txt
grep -Ec '^[-*0-9]' lab/ps/checklist.md
grep -Eic 'whatif' lab/ps/whatif.txt
awk -F, 'NR>1 && NF>=3 {n++} END {print n" extension(s)"}' lab/ps/extensions.csv
Six or more checklist items, a policy note naming an actual policy value, and -WhatIf output showing what would have happened. If lab/ps/whatif.txt is empty, the script ran without the switch and the directory is gone.
Notes
Appending -WhatIf first is worth building into a habit now. It is the single most valuable feature of the language for a support role, and it costs six characters.
This is an independent study companion for CompTIA A+ Core 2 220-1202 and is not produced by or endorsed by CompTIA.