February 2019
Intermediate to advanced
626 pages
15h 51m
English
The AST in PowerShell is available for any script block; an example is as follows:
{ Write-Host 'content' }.Ast
The script block that defines a function can be retrieved via Get-Command:
function Write-Content { Write-Host 'content' }
(Get-Command Write-Content).ScriptBlock
Or, the script block defining a function can be retrieved using Get-Item:
function Write-Content { Write-Host 'content' }(Get-Item function:\Write-Content).ScriptBlock
It is possible to work down through the content of the script block using AST. For example, the first argument for the Write-Host command might be accessed, as follows:
{ Write-Host 'content' }.Ast.
Endblock.
Statements.
PipelineElements.
CommandElements[1]
The preceding approach is rough, and simply ...
Read now
Unlock full access