-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathGet-MBFunction.ps1
More file actions
76 lines (67 loc) · 2.27 KB
/
Copy pathGet-MBFunction.ps1
File metadata and controls
76 lines (67 loc) · 2.27 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
function Get-MBFunction {
<#
.SYNOPSIS
Enumerates all functions within lines of code.
.DESCRIPTION
Enumerates all functions within lines of code.
.PARAMETER Code
Multiline or piped lines of code to process.
.PARAMETER Name
Name of a function to return. Default is all functions.
.EXAMPLE
TBD
.NOTES
Author: Zachary Loeber
Site: http://www.the-little-things.net/
Requires: Powershell 3.0
Version History
1.0.0 - Initial release
.LINK
http://www.the-little-things.net
#>
[CmdletBinding()]
param(
[parameter(Position = 0, Mandatory = $true, ValueFromPipeline=$true, HelpMessage='Lines of code to process.')]
[AllowEmptyString()]
[string[]]$Code,
[parameter(Position=1, HelpMessage='Name of function to process.')]
[string]$Name = '*'
)
begin {
$ThisFunc = $MyInvocation.MyCommand.Name
Write-Verbose "$($ThisFunc): Begin."
$Codeblock = @()
$ParseError = $null
$Tokens = $null
$predicate = {
($args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst]) -and
($args[0].Name -like $name)
}
}
process {
$Codeblock += $Code
}
end {
$ScriptText = ($Codeblock | Out-String).trim("`r`n")
Write-Verbose "$($ThisFunc): Attempting to parse AST."
$AST = [System.Management.Automation.Language.Parser]::ParseInput($ScriptText, [ref]$Tokens, [ref]$ParseError)
if($ParseError) {
throw "$($ThisFunc): Will not work properly with errors in the script, please modify based on the above errors and retry."
}
# First get all blocks
$Blocks = $AST.FindAll($predicate, $true)
Foreach ($Block in $Blocks) {
$FunctionProps = @{
Name = $Block.Name
Definition = $Block.Extent.Text
IsEmbedded = $false
AST = $Block
}
if (@(Get-MBParentASTType $Block) -contains 'FunctionDefinitionAst') {
$FunctionProps.IsEmbedded = $true
}
New-Object -TypeName psobject -Property $FunctionProps
}
Write-Verbose "$($ThisFunc): End."
}
}