Chapter 22: Using someone else’s script
This introduction explains why understanding and reusing other people’s PowerShell scripts matters, even when those scripts are imperfect or contain unfamiliar features.
After this chapter you will be able to
- Understanding borrowed scripts. Explain why interpreting someone else’s PowerShell script is a useful skill and why unfamiliar features need not prevent you from figuring out what a script does.
- Get-AdExistence script requirements. Describe what Get-AdExistence.ps1 is designed to work with, where it can run, and whether those prerequisites are needed to follow the explanation.
- Mandatory Computers parameter. Explain that the parameter block defines a mandatory -Computers parameter that accepts an array, so the caller must supply it when running the script.
- Computer information output flow. Trace how the script checks for a computer in Active Directory, stores information, and writes the result to the screen.
- Variable tracking during script review. Describe how to track variables while reading a script and use their likely values to predict what commands will do.
- Investigating every unfamiliar line. Explain how to investigate unfamiliar commands or syntax without skipping lines, and why that matters when adapting a script.
What it builds on
- Creating a parameterized script (chapter 19) · Creating a parameterized script introduced parameter blocks.
- Storing values in variables (chapter 16) · Storing values in variables.
- Write-Output (chapter 17) · Writing output with Write-Output.
- Asking for help (chapter 3) · Asking PowerShell for help with commands
Section by section
Introduction
This introduction explains why understanding and reusing other people’s PowerShell scripts matters, even when those scripts are imperfect or contain unfamiliar features.
- Understanding borrowed scripts: Explain why interpreting someone else’s PowerShell script is a useful skill and why unfamiliar features need not prevent you from figuring out what a script does.
Why it matters: It gives you a reason to examine useful scripts rather than discard them when parts look unfamiliar.
Key terms: PowerShell script, advanced PowerShell features
22.1 The script
Introduces a real-world script found online and states the environment it needs to run, while noting that readers can study it without that environment.
- Get-AdExistence script requirements: Describe what Get-AdExistence.ps1 is designed to work with, where it can run, and whether those prerequisites are needed to follow the explanation.
Why it matters: You can decide whether you can run the script while still learning from its explanation if you cannot.
Key terms: Get-AdExistence.ps1, Microsoft’s AD cmdlets, Windows-based computer, Active Directory
22.1.1 Parameter block
Identifies what the script’s parameter block requires and gives a basic orientation to its process block.
- Mandatory Computers parameter: Explain that the parameter block defines a mandatory -Computers parameter that accepts an array, so the caller must supply it when running the script.
Why it matters: This tells you what input the script needs before it can run. - Process block location: State that the process block is where the main substance of the script belongs, without claiming the section explains how it works.
Why it matters: It helps you recognize where to look for the script’s main work.
Key terms: parameter block, -Computers, array, mandatory, process block
22.1.2 Process block
This subsection explains how the script checks Active Directory, collects computer information, and outputs it, while briefly introducing Try/Catch.
- Computer information output flow: Trace how the script checks for a computer in Active Directory, stores information, and writes the result to the screen.
Why it matters: Following the data from the AD check to the output helps you understand what the script reports. - Basic Try Catch behavior: Explain, at the introductory level given here, what Try and Catch do when an attempted action fails.
Why it matters: This basic distinction helps you follow the script before error handling is covered in detail.
Key terms: $comp, $properties, hash table, PS custom object, Write-Output, Try, Catch
22.2 It’s a line-by-line examination
This section gives a practical method for understanding someone else’s PowerShell script line by line so you can adapt it to your needs.
- Variable tracking during script review: Describe how to track variables while reading a script and use their likely values to predict what commands will do.
Why it matters: A record of likely variable values makes an unfamiliar script easier to follow. - Investigating every unfamiliar line: Explain how to investigate unfamiliar commands or syntax without skipping lines, and why that matters when adapting a script.
Why it matters: Investigating each line helps you change the right part of a script while keeping experiments away from production.
Key terms: variables, command parameters, Get- commands, help, virtual machine, wildcards
22.3 Lab
22.4 Lab answer
Where this is used later
- Handling exceptions (chapter 24) · Develops the brief Try/Catch introduction into exception handling.
Concepts explained in this chapter
- try-catch statementTry/catch is an error-handling language construct that attempts operations and handles exceptions raised during execution.
- param blockA param block is a PowerShell language construct that declares the parameters and default values of a script or function.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
