Static analysis
Static analysis is the process of evaluating code without executing it. As mentioned in the introduction, PSScriptAnalyzer
uses static analysis.
In PowerShell, static analysis most often makes use of an Abstract Syntax Tree (AST): a tree-like representation of a piece of code. In PowerShell, an element of a script is represented by a node in the syntax tree. AST was introduced with PowerShell 3.
The largest elements represent the script itself, the root of the tree in effect. Each element added to the script is represented by a child node. For example, the parameter block is described by a ParamBlockAst
object, an individual parameter by a ParameterAst
, and so on.
A simple script block can be used as an example:
$scriptBlock = {
param ( $String )
Write-Host $String
}
This simple script block contains the nodes shown in Figure 21.1:
If the script had more than one parameter, more ParameterAst
nodes would...