Skip to content

Get-AstFunction

SYNOPSIS

Retrieves function definitions from a PowerShell script or Ast.

SYNTAX

Ast (Default)

Get-AstFunction [-Name <String>] -Ast <Ast> [-Recurse] [-ProgressAction <ActionPreference>]
 [<CommonParameters>]

Path

Get-AstFunction [-Name <String>] -Path <String> [-Recurse] [-ProgressAction <ActionPreference>]
 [<CommonParameters>]

Script

Get-AstFunction [-Name <String>] -Script <String> [-Recurse] [-ProgressAction <ActionPreference>]
 [<CommonParameters>]

DESCRIPTION

This function extracts function definitions from a given PowerShell script file, script content, or an existing Ast (Abstract Syntax Tree) object. It supports searching by function name and can optionally search within nested functions and script block expressions.

EXAMPLES

EXAMPLE 1

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

Output:

Ast    : {FunctionDefinitionAst, FunctionDefinitionAst}
Tokens : {...}
Errors : {}

Retrieves function definitions from the specified script file.

EXAMPLE 2

Get-AstFunction -Script "$scriptContent"

Output:

Ast    : {FunctionDefinitionAst}
Tokens : {...}
Errors : {}

Parses and retrieves function definitions from the provided script content.

EXAMPLE 3

$ast = Get-AstScript -Path "C:\Scripts\MyScript.ps1" | Select-Object -ExpandProperty Ast
Get-AstFunction -Ast $ast

Output:

Ast    : {FunctionDefinitionAst}
Tokens : {...}
Errors : {}

Extracts function definitions from an existing Ast object.

PARAMETERS

-Name

The name of the function to search for. Defaults to all functions ('*').

Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: *
Accept pipeline input: False
Accept wildcard characters: False

-Path

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

Type: String
Parameter Sets: Path
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False

-Script

The PowerShell script to be parsed.

Type: String
Parameter Sets: Script
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False

-Ast

An existing Ast object to search.

Type: Ast
Parameter Sets: Ast
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False

-Recurse

Search nested functions and script block expressions.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-ProgressAction

{{ Fill ProgressAction Description }}

Type: ActionPreference
Parameter Sets: (All)
Aliases: proga

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

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

INPUTS

OUTPUTS

PSCustomObject

NOTES

Contains Ast objects, tokenized script content, and parsing errors if any.

https://psmodule.io/Ast/Functions/Core/Get-AstFunction