2016-08-12 5 views
-1

私はPOWERシェルの世界に戻ってVBスクリプトから来ています 現在、私は メインメニューと複数サブのメインメニューでコンソールアプリケーションを開発中です。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

ありがとう、私はそれを試みるつもりです。ここに私のコードの例です –

関連する問題