-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGet-AstScript.ps1
More file actions
154 lines (128 loc) · 5.6 KB
/
Copy pathGet-AstScript.ps1
File metadata and controls
154 lines (128 loc) · 5.6 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
function Get-AstScript {
<#
.SYNOPSIS
Parses a PowerShell script or script file and returns its abstract syntax tree (Ast).
.DESCRIPTION
The Get-AstScript function parses a PowerShell script or script file and returns its abstract syntax tree (Ast),
along with tokens and errors encountered during parsing. This function can be used to analyze the structure
of a script by specifying either the script content directly or the path to a script file.
.EXAMPLE
Get-AstScript -Path "C:\\Scripts\\example.ps1"
Output:
```powershell
Ast : [System.Management.Automation.Language.ScriptBlockAst]
Tokens : {Token1, Token2, ...}
Errors : {Error1, Error2, ...}
```
Parses the PowerShell script located at "C:\\Scripts\\example.ps1" and returns its Ast, tokens, and any parsing errors.
.EXAMPLE
Get-AstScript -Script "Write-Host 'Hello World'"
Output:
```powershell
Ast : [System.Management.Automation.Language.ScriptBlockAst]
Tokens : {Token1, Token2, ...}
Errors : {}
```
Parses the provided PowerShell script string and returns its Ast, tokens, and any parsing errors.
.EXAMPLE
Get-AstScript -Path "C:\\Scripts" -Recurse
Parses all PowerShell script files in the "C:\\Scripts" directory and its subdirectories.
.EXAMPLE
Get-AstScript -Path @("C:\\Scripts\\example.ps1", "C:\\Scripts\\example2.ps1")
Parses multiple PowerShell script files and returns their Asts.
.EXAMPLE
Get-AstScript -Script @("Write-Host 'Hello'", "Write-Host 'World'")
Parses multiple PowerShell script strings and returns their Asts.
.EXAMPLE
"Write-Host 'Hello'", "Write-Host 'World'" | Get-AstScript
Parses multiple PowerShell script strings from the pipeline and returns their Asts.
.EXAMPLE
Get-ChildItem -Path "C:\\Scripts" -Filter "*.ps1" | Get-AstScript
Parses all PowerShell script files returned by Get-ChildItem.
.OUTPUTS
PSCustomObject
.NOTES
The returned custom object contains the following properties:
- `Ast` - [System.Management.Automation.Language.ScriptBlockAst]. The abstract syntax tree (Ast) of the parsed script.
- `Tokens` - [System.Management.Automation.Language.Token[]]. The tokens generated during parsing.
- `Errors` - [System.Management.Automation.Language.ParseError[]]. Any parsing errors encountered.
.LINK
https://psmodule.io/Ast/Functions/Core/Get-AstScript/
#>
[outputType([System.Management.Automation.Language.ScriptBlockAst])]
[CmdletBinding(DefaultParameterSetName = 'PipelineInput')]
param (
# The path(s) to PowerShell script file(s) or folder(s) to be parsed.
[Parameter(
Mandatory,
ParameterSetName = 'Path'
)]
[ValidateScript({
foreach ($p in $_) {
if (-not (Test-Path -Path $p)) { return $false }
}
return $true
})]
[string[]] $Path,
# Process directories recursively
[Parameter(ParameterSetName = 'Path')]
[Parameter(ParameterSetName = 'PipelineInput')]
[switch] $Recurse,
# The PowerShell script(s) to be parsed.
[Parameter(
Mandatory,
ParameterSetName = 'Script'
)]
[string[]] $Script,
# Input from pipeline that will be automatically detected as path or script
[Parameter(
Mandatory,
ValueFromPipeline,
ParameterSetName = 'PipelineInput'
)]
[string[]] $InputObject
)
begin {}
process {
switch ($PSCmdlet.ParameterSetName) {
'Path' {
foreach ($p in $Path) {
# Check if the path is a directory
if (Test-Path -Path $p -PathType Container) {
Read-AstDirectory -DirPath $p -RecurseDir $Recurse
} else {
# Path is a file
Read-AstScriptFile -FilePath $p
}
}
}
'Script' {
foreach ($scriptContent in $Script) {
Read-AstScriptContent -ScriptContent $scriptContent
}
}
'PipelineInput' {
# Default parameter set for handling pipeline input
if ($null -ne $InputObject) {
foreach ($item in $InputObject) {
# Check if input is a file path or directory
if (Test-Path -Path $item -ErrorAction SilentlyContinue) {
if (Test-Path -Path $item -PathType Container) {
Read-AstDirectory -DirPath $item -RecurseDir $Recurse
} else {
Read-AstScriptFile -FilePath $item
}
} elseif ($PSBoundParameters.ContainsKey('InputObject') -and
$InputObject.PSObject.Properties.Name -contains 'FullName' -and
(Test-Path -Path $InputObject.FullName -ErrorAction SilentlyContinue)) {
Read-AstScriptFile -FilePath $InputObject.FullName
} else {
Read-AstScriptContent -ScriptContent $item
}
}
}
}
}
}
end {}
}