2016-08-12 2 views
-1

저는 VBL 스크립트에서 powershell 의 세계로 돌아 왔습니다. 현재는 주 메뉴 및 다중 하위 기본 메뉴가있는 콘솔 응용 프로그램을 개발하는 중입니다.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

감사합니다. 여기에 내 코드 예제가있다. –