This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
/
GetScriptFunctions.ps1
61 lines (52 loc) · 2.03 KB
/
GetScriptFunctions.ps1
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
function GetScriptFunctions {
<#
.Synopsis
Outputs all functions within a Script or ScriptBlock.
.Description
Uses AST to retrieve all functions from a valid script file,
then outputs them all. Useful for dot-sourcing functions from
a file that also contains running code.
.Example
GetScriptFunctions -Export -ScriptPath .\testscript.ps1 | iex
.Example
(GetScriptFunctions -ScriptPath .\testscript.ps1).Count
7
.Example
Get-ChildItem -Recurse -Filter *.ps1 | GetScriptFunctions | Set-Content 'functions.psm1'
#>
[OutputType([System.Management.Automation.Language.Ast[]])]
param(
# FilePath for script to analyse
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline, Position = 0)]
[ValidateScript({Test-Path $_})]
[Alias('PSPath')]
[string[]]$ScriptPath,
# To export the function(s) extent alone, or with full AST data
[switch]$Export
)
process {
foreach ($Item in Convert-Path $ScriptPath) {
try {
Write-Verbose "Analysing [$Item]"
$Tokens, $Errors = @()
$Script = [System.Management.Automation.Language.Parser]::ParseFile($Item, [ref]$Tokens, [ref]$Errors)
$Errors | ForEach-Object {
Write-Error -Message $_ -ErrorAction Stop
}
} catch {
Write-Error "Failed to parse File '$($Item)'.`n$_"
}
try {
$functions = $Script.FindAll( {$args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst]}, $true)
Write-Verbose "Found $($functions.count) functions."
} catch {
Write-Error "Failed to find any functions in File '$($Item)'.`n$_"
}
if ($Export) {
$functions.Extent
} else {
$functions
}
}
}
}