2016-08-12 174 views
-1

我来自一个VB脚本回到Powerhell的世界 当前我正在开发一个控制台应用程序与主菜单和多子主菜单 的过程。Powershell多选菜单和子菜单

我想菜单系统例如像一个连带效应的工作:

--------------------------------------------------------------------- 
        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:" 
+0

如果你显示你已经尝试了什么,并且出了什么问题,你可能会有更好的运气获得帮助。 – Jeff

回答

2

有你可以创建一个菜单系统的几种方法。就个人而言,我已经使用这种格式在过去为驱动的菜单,TUI脚本:

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) 。

唯一可能令人困惑的部分是在您的代码执行等待用户按下任意键后,我在那里的暂停。如果你没有那个,脚本将清除控制台,并在代码执行后返回到菜单。我把它放在里面,以便在返回到菜单系统之前查看输出。

+0

谢谢,我将尝试。这里是我的代码的一个例子 –