Skip to content

Instantly share code, notes, and snippets.

@amnich
Created September 4, 2017 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amnich/5099c5e472150da1d09f5ceb1142765a to your computer and use it in GitHub Desktop.
Save amnich/5099c5e472150da1d09f5ceb1142765a to your computer and use it in GitHub Desktop.
function menu($functionName) {
$AST = [System.Management.Automation.Language.Parser]::ParseFile(
'C:\Temp\profile.ps1',
[ref]$null,
[ref]$Null
)
$functions = @($AST.FindAll({
$args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst]}
, $true) | ForEach-Object {
if ($_.Extent.Text -match '^\s{0,}function ([\w|\-]*)\s{0,}{{0,1}'){
$Matches[1].trim() }
} | Where-Object {$_ -ne "menu" -and $_ -like "*$functionName*"} | Sort-Object)
For ($i=0;$i -lt $functions.Count;$i++){
Write-Output " [$($i.tostring('00'))] $($functions[$i])"
}
if ($functions.count -gt 0){
$run = "RUN"
while ($run -as [int] -eq $null){
$Run = Read-Host -Prompt "Function to run"
}
if ($functions[$run]){
Write-Warning "Run $($functions[$run]) [N to stop]"
$confirm = Read-Host
if ($confirm -notlike "N*"){
. $functions[$run]
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment