PowerShell PSScriptAnalyzer Concepts
PSScriptAnalyzer concepts are the ideas behind a checker that reads PowerShell code for rule breaks. This lesson names the rules only, and no analyzer run is shown.
An approved verb follows the verb rule
Get is an approved verb, so a function named Get-Greeting follows the verb rule. The function prints by implicit output.
function Get-Greeting {
param([string]$Name = "Ana")
"Hello, $Name"
}
Get-Greeting -Name "Ana"Every parameter must be used
The unused parameter rule flags a parameter that is never used. Here the Number parameter is used in the calculation.
function Get-Double {
param([int]$Number)
$Number * 2
}
Get-Double -Number 21Clean output and tidy layout
Implicit output keeps the code clear of Write-Host, and steady indentation keeps the layout tidy. Both follow the rules above.
function Get-Total {
param([int]$First, [int]$Second)
$First + $Second
}
Get-Total -First 3 -Second 4Example
function Get-Report {
param([string]$Title, [int]$Tasks)
"$Title has $Tasks tasks"
}
Get-Report -Title "Weekly" -Tasks 3Key points
- PSScriptAnalyzer: a checker that reads PowerShell code and reports rule breaks, without running the code.
- Rule: one check the checker applies, like a check on verb names or unused parameters.
- PSUseApprovedVerbs: the rule that checks function verbs come from the approved verb list.
- PSReviewUnusedParameter: the rule that checks every parameter is actually used.
- PSAvoidUsingWriteHost: the rule that checks code does not use Write-Host for output.
- PSAvoidUsingPlainTextForPassword: the rule that checks a password is not written as plain readable text in code.
- Formatting rule: a rule that checks tidy layout, like consistent indentation.
In this example, Get is an approved verb, both parameters are used in the result, and the result prints by implicit output in a tidily laid out function.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Which rule name checks that a function verb comes from the approved verb list?