Skip to content

Get-AstCommand

SYNOPSIS

Retrieves command Ast (Abstract Syntax Tree) elements from a PowerShell script or Ast object.

SYNTAX

Ast (Default)

Get-AstCommand -Ast <Ast> [-Name <string>] [-Recurse] [<CommonParameters>]

Path

Get-AstCommand -Path <string> [-Name <string>] [-Recurse] [<CommonParameters>]

Script

Get-AstCommand -Script <string> [-Name <string>] [-Recurse] [<CommonParameters>]

ALIASES

This cmdlet has the following aliases, {{Insert list of aliases}}

DESCRIPTION

This function extracts and returns command Ast elements from a specified PowerShell script file, script content, or an existing Ast object. The function supports multiple input methods, including direct script text, file paths, or existing Ast objects. It also provides an option to search nested functions and script block expressions.

EXAMPLES

EXAMPLE 1

Get-AstCommand -Path "C:\Scripts\MyScript.ps1"

Output:

Ast    : {@{Name=Get-Process; Extent=...}, @{Name=Write-Host; Extent=...}}
Tokens : {...}
Errors : {}

Parses the specified script file and extracts command Ast elements.

EXAMPLE 2

Get-AstCommand -Script "Get-Process; Write-Host 'Hello'"

Output:

Ast    : {@{Name=Get-Process; Extent=...}, @{Name=Write-Host; Extent=...}}
Tokens : {...}
Errors : {}

Parses the provided script content and extracts command Ast elements.

EXAMPLE 3

$ast = [System.Management.Automation.Language.Parser]::ParseInput("Get-Process", [ref]$null, [ref]$null) Get-AstCommand -Ast $ast

Output:

Ast    : {@{Name=Get-Process; Extent=...}}
Tokens : {...}
Errors : {}

Extracts command Ast elements from a manually parsed Ast object.

PARAMETERS

-Ast

An existing Ast object to search.

Type: System.Management.Automation.Language.Ast
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: Ast
  Position: Named
  IsRequired: true
  ValueFromPipeline: true
  ValueFromPipelineByPropertyName: true
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-Name

The name of the command to search for. Defaults to all commands ('*').

Type: System.String
DefaultValue: '*'
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-Path

The path to the PowerShell script file to be parsed. Validate using Test-Path

Type: System.String
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: Path
  Position: Named
  IsRequired: true
  ValueFromPipeline: true
  ValueFromPipelineByPropertyName: true
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-Recurse

Search nested functions and script block expressions.

Type: System.Management.Automation.SwitchParameter
DefaultValue: False
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-Script

The PowerShell script to be parsed.

Type: System.String
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: Script
  Position: Named
  IsRequired: true
  ValueFromPipeline: true
  ValueFromPipelineByPropertyName: true
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

System.String

{{ Fill in the Description }}

System.Management.Automation.Language.Ast

{{ Fill in the Description }}

OUTPUTS

PSCustomObject

{{ Fill in the Description }}

NOTES

Returns an object containing extracted Ast elements, tokens, and errors.