Thursday, March 28, 2024
HomePowershellUse the Summary Syntax Tree (AST) to examine PowerShell command syntax in...

Use the Summary Syntax Tree (AST) to examine PowerShell command syntax in scripts · Mike F. Robbins


I not too long ago wanted to find out the PowerShell instructions and parameters utilized in a number of scripts. What
higher approach to accomplish this activity than to make use of the Summary Syntax Tree (AST)?

The Get-MrSyntax perform begins by requiring not less than PowerShell model 3.0. That is the oldest
model that exposes the AST.

The Path parameter makes use of ValidateScript for parameter validation to solely settle for information with a PS1
or PSM1 extension. The trail(s) to the script information could be specified through pipeline or parameter enter.

The perform iterates by means of every file and every command throughout the file to return the outcomes. For
a extra concise record of outcomes, I made a decision to not unroll the parameters utilized by the instructions.

#Requires -Model 3.0
perform Get-MrSyntax {

<#
.SYNOPSIS
    Listing PowerShell instructions and parameters within the specified PowerShell script.

.DESCRIPTION
    Get-MrSyntax is a PowerShell perform that makes use of the Summary Syntax Tree (AST) to find out the
    instructions and parameters inside a PowerShell script.

.PARAMETER Path
    Path to certainly one of extra PowerShell PS1 or PSM1 script information.

.EXAMPLE
    Get-MrSyntax -Path C:ScriptsMyScript.ps1

.EXAMPLE
    Get-ChildItem -Path C:Scripts*.ps1 | Get-MrSyntax

.EXAMPLE
    Get-MrSyntax -Path (Get-ChildItem -Path C:Scripts*.ps1)

.NOTES
    Writer:  Mike F Robbins
    Web site: https://mikefrobbins.com
    Twitter: @mikefrobbins
#>

    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline)]
        [ValidateScript({
            If (Test-Path -Path $_ -PathType Leaf -Include *.ps1, *.psm1) {
                $True
            } else {
                Throw "'$_' is not a valid PowerShell PS1 or PSM1 script file."
            }
        })]
        [string[]]$Path
    )

    PROCESS {

        foreach ($file in $Path) {
            $AST = [System.Management.Automation.Language.Parser]::ParseFile($File, [ref]$null, [ref]$null)

            $AST.FindAll({$args[0].GetType().Title -like 'CommandAst'}, $true) |
            ForEach-Object {
                [pscustomobject]@{
                    Cmdlet = $_.CommandElements[0].Worth
                    Parameters = $_.CommandElements.ParameterName
                    File = $file
                }
            }
        }
    }
}

The Get-MrSyntax perform proven on this weblog article is a part of
my MrInspector PowerShell module. You’ll be able to
set up it from the PowerShell Gallery utilizing the next command.

Set up-Module -Title MrInspector

The video proven under demonstrates tips on how to use this module.

You’ll be able to obtain the supply code from
my Inspector GitHub repo. Be happy to submit a GitHub
problem when you’ve got solutions or discover issues. I settle for pull requests if you want to contribute.

µ

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments