-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGet-AstFunctionName.ps1
More file actions
106 lines (84 loc) · 2.85 KB
/
Copy pathGet-AstFunctionName.ps1
File metadata and controls
106 lines (84 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
function Get-AstFunctionName {
<#
.SYNOPSIS
Retrieves the names of functions from an abstract syntax tree (Ast) in a PowerShell script.
.DESCRIPTION
Parses a PowerShell script file or script content to extract function names using an abstract syntax tree (Ast).
The function supports searching by name, parsing from a file path, or directly from a script string. It can also
search within nested functions and script block expressions when the -Recurse switch is used.
.EXAMPLE
Get-AstFunctionName -Path "C:\Scripts\example.ps1"
Output:
```powershell
Get-Data
Set-Configuration
```
Extracts function names from the specified PowerShell script file.
.EXAMPLE
Get-AstFunctionName -Script "function Test-Function { param($x) Write-Host $x }"
Output:
```powershell
Test-Function
```
Extracts function names from the given script string.
.EXAMPLE
Get-AstFunctionName -Path "C:\Scripts\example.ps1" -Recurse
Output:
```powershell
Get-Data
Set-Configuration
Helper-Function
```
Extracts function names from the specified script file, including nested functions.
.OUTPUTS
System.String
Description is here?
.NOTES
The name of each function found in the PowerShell script.
.LINK
https://psmodule.io/Ast/Functions/Functions/Get-AstFunctionName/
#>
[CmdletBinding()]
param (
# The name of the command to search for. Defaults to all commands ('*').
[Parameter()]
[string] $Name = '*',
# The path to the PowerShell script file to be parsed.
# Validate using Test-Path
[Parameter(
Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName,
ParameterSetName = 'Path'
)]
[ValidateScript({ Test-Path -Path $_ })]
[string] $Path,
# The PowerShell script to be parsed.
[Parameter(
Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName,
ParameterSetName = 'Script'
)]
[string] $Script,
# Search nested functions and script block expressions.
[Parameter()]
[switch] $Recurse
)
begin {}
process {
switch ($PSCmdlet.ParameterSetName) {
'Path' {
$functionAst = Get-AstFunction -Name $Name -Path $Path -Recurse:$Recurse
}
'Script' {
$functionAst = Get-AstFunction -Name $Name -Script $Script -Recurse:$Recurse
}
}
# Process each function and extract the name
$functionAst.Ast | ForEach-Object {
$_.Name
}
}
end {}
}