-
-
Save perXautomatik/8d3326130d5d783d6d03c2207ff74126 to your computer and use it in GitHub Desktop.
Extract a list of functions names and definitions inside a script, listing line numbers function begin and end;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
param( | |
# The script or file path to parse | |
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] | |
[Alias("Path", "PSPath")] | |
$Script | |
) | |
process { | |
Write-Progress "Parsing $Script" | |
Write-Debug " ENTER: Get-ParseResults $Script" | |
$ParseErrors = $null | |
$Tokens = $null | |
if ($Script | Test-Path -ErrorAction SilentlyContinue) { | |
Write-Debug " Parse $Script as Path" | |
$AST = [System.Management.Automation.Language.Parser]::ParseFile(($Script | Convert-Path), [ref]$Tokens, [ref]$ParseErrors) | |
} elseif ($Script -is [System.Management.Automation.FunctionInfo]) { | |
Write-Debug " Parse $Script as Function" | |
$String = "function $($Script.Name) { $($Script.Definition) }" | |
$AST = [System.Management.Automation.Language.Parser]::ParseInput($String, [ref]$Tokens, [ref]$ParseErrors) | |
} else { | |
Write-Debug " Parse $Script as String" | |
$AST = [System.Management.Automation.Language.Parser]::ParseInput([String]$Script, [ref]$Tokens, [ref]$ParseErrors) | |
} | |
#find all returns each node matching criteria, as [System.Management.Automation.Language.CommandAst] | |
$searchNestedScriptBlocks = $true | |
$AST.FindAll({$args[0].GetType().Name -like 'FunctionDefinitionAst'}, $searchNestedScriptBlocks) | | |
ForEach-Object { | |
[pscustomobject]@{ | |
Name = $_.name | |
Parameters = $_.Parameters | |
#body = $_.Body | |
StartLine = $_.extent.StartLineNumber | |
EndLine = $_.extent.EndLineNumber | |
extent = $_.extent.text | |
} | |
} | Sort-Object name | |
#| Group-Object name,parameters -AsHashTable -AsString ; $q.Values.Extent | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Use the Abstract Syntax Tree (AST) to inspect PowerShell command syntax in scripts source : https://mikefrobbins.com/2022/04/08/use-the-abstract-syntax-tree-ast-to-inspect-powershell-command-syntax-in-scripts/ | |
$file = 'C:\Users\crbk01\Documents\PowerShell\dependensies.psm1' | |
$AST = [System.Management.Automation.Language.Parser]::ParseFile($File, [ref]$null, [ref]$null) | |
#find all returns each node matching criteria, as [System.Management.Automation.Language.CommandAst] | |
$searchNestedScriptBlocks = $false | |
$AST.FindAll({$args[0].GetType().Name -like 'CommandAst'}, $searchNestedScriptBlocks) | | |
ForEach-Object { | |
[pscustomobject]@{ | |
Cmdlet = $_.CommandElements[0].Value | |
Parameters = $_.CommandElements.ParameterName | |
Other = $_.CommandElements | |
#Other2 = $_.Parent | |
otherq = $_.extent | |
File = $file | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment