There are a lot of WPF-based PowerShell menu functions and demos available in various places, but for some reason I feel that PowerShell really needs a good, ol' fashion CLI menu.
So, here is a quick, sort of hack'ish CLI menu for PowerShell (dont worry, I just made up the individual menu options - can you tell I just reread BOFH recently?).
Use the up and down arrows to navigate the menu, and press ENTER to select.
function DrawMenu {
## supportfunction to the Menu function below
param ($menuItems, $menuPosition, $menuTitel)
$fcolor = $host.UI.RawUI.ForegroundColor
$bcolor = $host.UI.RawUI.BackgroundColor
$l = $menuItems.length + 1
cls
$menuwidth = $menuTitel.length + 4
Write-Host "`t" -NoNewLine
Write-Host ("*" * $menuwidth) -fore $fcolor -back $bcolor
Write-Host "`t" -NoNewLine
Write-Host "* $menuTitel *" -fore $fcolor -back $bcolor
Write-Host "`t" -NoNewLine
Write-Host ("*" * $menuwidth) -fore $fcolor -back $bcolor
Write-Host ""
Write-debug "L: $l MenuItems: $menuItems MenuPosition: $menuposition"
for ($i = 0; $i -le $l;$i++) {
Write-Host "`t" -NoNewLine
if ($i -eq $menuPosition) {
Write-Host "$($menuItems[$i])" -fore $bcolor -back $fcolor
} else {
Write-Host "$($menuItems[$i])" -fore $fcolor -back $bcolor
}
}
}
function Menu {
## Generate a small "DOS-like" menu.
## Choose a menuitem using up and down arrows, select by pressing ENTER
param ([array]$menuItems, $menuTitel = "MENU")
$vkeycode = 0
$pos = 0
DrawMenu $menuItems $pos $menuTitel
While ($vkeycode -ne 13) {
$press = $host.ui.rawui.readkey("
$vkeycode = $press.virtualkeycode
Write-host "$($press.character)" -NoNewLine
If ($vkeycode -eq 38) {$pos--}
If ($vkeycode -eq 40) {$pos++}
if ($pos -lt 0) {$pos = 0}
if ($pos -ge $menuItems.length) {$pos = $menuItems.length -1}
DrawMenu $menuItems $pos $menuTitel
}
Write-Output $($menuItems[$pos])
}
Example:
$bad = "Format c:","Send spam to boss","Truncate database *","Randomize user password","Download dilbert","Hack local AD"
$selection = Menu $bad "WHAT DO YOU WANNA DO?"
Write-Host "YOU SELECTED : $selection ... DONE!`n"
Another Example:
$options = "Dir","Ping", "Ipconfig"
$selection = Menu $options "CHOOSE YOUR COMMAND:"
Switch ($selection) {
"Dir" {Invoke-Expression "Dir C:\";break}
"Ping" {Invoke-Expression "Ping 127.0.0.1";break}
"Ipconfig" {Invoke-Expression "Ipconfig";break}
}
7 comments:
That's cool!
Nice.. I like the menu options :)
Switch the
if ($pos -lt 0) {$pos = 0}with
if ($pos -lt 0) {$pos = $menuItems.length -1}and
if ($pos -ge $menuItems.length) {$pos = $menuItems.length -1}with
if ($pos -ge $menuItems.length) {$pos = 0}to allow the menu to wrap around.
Very cool script !!! I love it !
Retreive it here: http://powershell-scripting.com/index.php?option=com_content&task=view&id=296&Itemid=71
Great script, nice and adaptable.
I had to modify the following color lines to get it to work
Before:
$fcolor = $host.UI.RawUI.ForegroundColor
$bcolor = $host.UI.RawUI.BackgroundColor
After:
$fcolor = "Black"
$bcolor = "Green"
Obviously you can go anyway you want with that though.
Cool.
I'm real new to PS.
But I needed a script like this, but with option e.g:
OPEN FILE X
OPEN FILE Y
OPEN FILE Z
But I dont know how to do it.
Can you adapat this script to do so? THaaanks :D
Hi .:TDA:.
I have added another example to my post.
Does this example solve your problem?
Post a Comment