Powershell 多选菜单和子菜单 [英] Powershell Multi-choice Menu and Sub menu
问题描述
我从一个 vb 脚本回到了 powershell 的世界目前我正在开发一个控制台应用程序一个主菜单和多个子主菜单.
I coming from a vb script back ground in to the world of powershell currently i am in the process of developing a console application with a main menu and multiple-sub main menus.
我希望菜单系统像级联效果一样工作,例如:
i would like the menu system to work like a cascade effect for example:
---------------------------------------------------------------------
Main Menu
clear
write-host "A"
write-host "B"
write-host "C"
write-host "D"
#When the user choose a letter the use would then
#forwarded to the letter A menu
$Choice = read-host ''"Please enter your Choice"
--------------------------------------------------------
Menu A
#If the user selects any of the options available from (A-Test1 to A-Test3) in #Menu A for example A-Test1:
#The menu A-Test1 will show up and display a menu of a list of
#actions to perform or return the user back to the menu A.
write-host "A-Test1"
write-host "A-Test2"
write-host "A-Test3"
write-host "A-Test4"
write-host "Exit " # Exit will return the user back to the main menu
# To select another choice.
$select = read-host ''"Please select from menu A:"
推荐答案
有多种方法可以创建菜单系统.就个人而言,我过去曾将这种格式用于菜单驱动的 TUI 脚本:
There are several ways you could create a menu system. Personally, I have used this format in the past for a menu driven, TUI script:
function mainMenu {
$mainMenu = 'X'
while($mainMenu -ne ''){
Clear-Host
Write-Host "`n`t`t My Script`n"
Write-Host -ForegroundColor Cyan "Main Menu"
Write-Host -ForegroundColor DarkCyan -NoNewline "`n["; Write-Host -NoNewline "1"; Write-Host -ForegroundColor DarkCyan -NoNewline "]"; `
Write-Host -ForegroundColor DarkCyan " Submenu1"
Write-Host -ForegroundColor DarkCyan -NoNewline "`n["; Write-Host -NoNewline "2"; Write-Host -ForegroundColor DarkCyan -NoNewline "]"; `
Write-Host -ForegroundColor DarkCyan " Submenu2"
$mainMenu = Read-Host "`nSelection (leave blank to quit)"
# Launch submenu1
if($mainMenu -eq 1){
subMenu1
}
# Launch submenu2
if($mainMenu -eq 2){
subMenu2
}
}
}
function subMenu1 {
$subMenu1 = 'X'
while($subMenu1 -ne ''){
Clear-Host
Write-Host "`n`t`t My Script`n"
Write-Host -ForegroundColor Cyan "Sub Menu 1"
Write-Host -ForegroundColor DarkCyan -NoNewline "`n["; Write-Host -NoNewline "1"; Write-Host -ForegroundColor DarkCyan -NoNewline "]"; `
Write-Host -ForegroundColor DarkCyan " Say hello"
Write-Host -ForegroundColor DarkCyan -NoNewline "`n["; Write-Host -NoNewline "2"; Write-Host -ForegroundColor DarkCyan -NoNewline "]"; `
Write-Host -ForegroundColor DarkCyan " Say goodbye"
$subMenu1 = Read-Host "`nSelection (leave blank to quit)"
$timeStamp = Get-Date -Uformat %m%d%y%H%M
# Option 1
if($subMenu1 -eq 1){
Write-Host 'Hello!'
# Pause and wait for input before going back to the menu
Write-Host -ForegroundColor DarkCyan "`nScript execution complete."
Write-Host "`nPress any key to return to the previous menu"
[void][System.Console]::ReadKey($true)
}
# Option 2
if($subMenu1 -eq 2){
Write-Host 'Goodbye!'
# Pause and wait for input before going back to the menu
Write-Host -ForegroundColor DarkCyan "`nScript execution complete."
Write-Host "`nPress any key to return to the previous menu"
[void][System.Console]::ReadKey($true)
}
}
}
function subMenu2 {
$subMenu2 = 'X'
while($subMenu2 -ne ''){
Clear-Host
Write-Host "`n`t`t My Script`n"
Write-Host -ForegroundColor Cyan "Sub Menu 2"
Write-Host -ForegroundColor DarkCyan -NoNewline "`n["; Write-Host -NoNewline "1"; Write-Host -ForegroundColor DarkCyan -NoNewline "]"; `
Write-Host -ForegroundColor DarkCyan " Show processes"
Write-Host -ForegroundColor DarkCyan -NoNewline "`n["; Write-Host -NoNewline "2"; Write-Host -ForegroundColor DarkCyan -NoNewline "]"; `
Write-Host -ForegroundColor DarkCyan " Show PS Version"
$subMenu2 = Read-Host "`nSelection (leave blank to quit)"
$timeStamp = Get-Date -Uformat %m%d%y%H%M
# Option 1
if($subMenu2 -eq 1){
Get-Process
# Pause and wait for input before going back to the menu
Write-Host -ForegroundColor DarkCyan "`nScript execution complete."
Write-Host "`nPress any key to return to the previous menu"
[void][System.Console]::ReadKey($true)
}
# Option 2
if($subMenu2 -eq 2){
$PSVersionTable.PSVersion
# Pause and wait for input before going back to the menu
Write-Host -ForegroundColor DarkCyan "`nScript execution complete."
Write-Host "`nPress any key to return to the previous menu"
[void][System.Console]::ReadKey($true)
}
}
}
mainMenu
您可以根据需要嵌套尽可能多的菜单,或将代码置于任何级别执行.从那以后,当我需要一个用户友好的工具时,我改用 WinForms,但这种格式已经为我服务了一段时间(我仍然将它用于我实验室中的一些脚本,我不想创建完整的 GUI).
You can nest as many menus deep as you need or put your code to execute at any level. I have since switched to using WinForms when I need a user-friendly tool, but this format has served me well for a while (and I still use it for some scripts in my lab where I don't feel like creating a full GUI).
唯一可能令人困惑的部分是在您的代码执行等待用户按任意键后我在那里的暂停.如果那里没有,脚本将清除控制台并在代码执行后返回菜单.我把它放进去,这样我就可以在返回菜单系统之前查看输出.
The only part that might be confusing is the pause I have in there after your code executes waiting for a user to press any key. If you don't have that in there, the script will clear the console and go back to the menu after your code executes. I put that in so that I can review the output before returning to the menu system.
这篇关于Powershell 多选菜单和子菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!