Last active
February 17, 2022 08:37
-
-
Save stephenbaidu/05a13756a0befa09518874ffa2224ea1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Function SimpleContextMenu { | |
param ( | |
[Parameter(Mandatory, Position = 0)] | |
[System.Object] | |
$List, | |
[Parameter(Mandatory = $false, Position = 1)] | |
[string] | |
$Header = "Select" | |
) | |
if (@("OrderedDictionary", "Hashtable", "Object[]") -notcontains $List.GetType().Name) | |
{ | |
Write-Host "Collection type not supported" | |
return | |
} | |
$choices = @{} | |
[Int32]$lastIndex = 0 | |
Write-Host $Header -ForegroundColor Blue | |
if ($List.GetType().IsArray) { | |
foreach ($item in $List) { | |
$choices[++$lastIndex] = $item | |
Write-Host " $($lastIndex): $item" | |
} | |
} else { | |
foreach ($item in $List.GetEnumerator()) { | |
if ($item.Name.StartsWith("__")) { # Keys with this prefix are treated as information and are good for headers and separators | |
Write-Host "$($item.Value)" -ForegroundColor Blue | |
} else { | |
$choices[++$lastIndex] = $item.Name | |
Write-Host " $($lastIndex): $($item.Value)" | |
} | |
} | |
} | |
Write-Host "" | |
Write-Host "Enter a number between [1, $lastIndex] to select command or <Enter> to ignore" -ForegroundColor Blue | |
[Int32]$choice = Read-Host | |
if (($choice -gt 0) -and ($choice -le $lastIndex)) { | |
return $choices[$choice] | |
} else { | |
return $null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment