question

akosijesyang avatar image
0 Votes"
akosijesyang asked akosijesyang commented

Tool Launcher Menu in PowerShell

Hello - I'm currently working on a PS script which launches different kind of tools similar to sconfig in Windows Server Core systems. Since the tool has main menu for tools selection, there's also sub-menu on it and I want to stay on that sub-menu even after selecting the tool I want to launch, and only go back when I selected the option to go back to the main menu.

Main menu - then selected #3:
141031-image.png


Then went to the Sub-menu:
140995-image.png

Selected Sub-Tool #1:
140996-image.png

Then pressed any key - went back to main menu - this is the part I want to stay in the sub-menu and only go back to main menu when I select "Return to main options...":
141033-image.png




Here's the code I'm currently working on - got the menu codes somewhere in the internet years ago, btw:

 [BOOLEAN]$global:xExitSession = $false
 Function LoadMenuSystem() {
     [INT]$xMenu1 = 0
     [INT]$xMenu2 = 0
     [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUserDeclaredVarsMoreThanAssignments', '', Scope = 'Function')]
     [BOOLEAN]$xValidSelection = $false
     while ( $xMenu1 -lt 1 -or $xMenu1 -gt 3) {
         Clear-Host
         Write-Host "`n"
         Write-Host "PLEASE SELECT TOOL TO LAUNCH:`n" -ForegroundColor Yellow
         Write-Host "`[1] Tool #1"
         Write-Host "`[2] Tool #2"
         Write-Host "`[3] Tool #3 and sub-options [...]" # Sub-menu
         [int]$xMenu1 = Read-host "`n`Please enter option 1 to 3..."
         if ( $xMenu1 -lt 1 -or $xMenu1 -gt 3) {
             Write-Host "`nPlease select one of the options available.`n" -ForegroundColor Red
             Start-Sleep -Seconds 1
         }
     }
     Switch ($xMenu1) {
         1 {
             Write-Host "Tool #1 launched..." -ForegroundColor Yellow
         }
         2 { 
             Write-Host "Tool #2 launched..." -ForegroundColor Yellow
         }
         3 {
             while ( $xMenu2 -lt 1 -or $xMenu2 -gt 3 ) {
                 Clear-Host
                 Write-Host "Tool-Sub-Menu:`n" -ForegroundColor Yellow
                 Write-Host "`[1] Sub-Tool #1"
                 Write-Host "`[2] Sub-Tool #2"
                 Write-Host "`[3] Return to main options [...]"
                 [int]$xMenu2 = Read-host "`n`Please enter option 1 to 3..."
                 if ( $xMenu2 -lt 1 -or $xMenu2 -gt 3 ) {
                     Write-Host "`nPlease select one of the options available.`n" -ForegroundColor Red
                     Start-Sleep -Seconds 1
                 }
             }
             Switch ($xMenu2) {
                 1 { 
                     Write-Host "Tool #3 Sub-option #1..." -ForegroundColor Yellow
                 }
                 2 {
                     Write-Host "Tool #3 Sub-option #2..." -ForegroundColor Yellow
                 }
                 3 { LoadMenuSystem }
             }
         }
            
         default {
             [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUserDeclaredVarsMoreThanAssignments', '', Scope = 'Function')]
             $global:xExitSession = $true; break 
         }
     }
 }
 LoadMenuSystem
 while ($xExitSession -ne 1) {
     write-host "`n`n################################## COMPLETED ##################################`n" -ForegroundColor Yellow
     Read-Host "Press any key to go back to option..."
     LoadMenuSystem
 }
windows-server-powershell
image.png (6.0 KiB)
image.png (5.5 KiB)
image.png (11.3 KiB)
image.png (5.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MotoX80 avatar image
0 Votes"
MotoX80 answered akosijesyang commented

Put the switch tests inside of the while loop and use it to validate the user input. Also put the submenu in it's own function.

  [BOOLEAN]$global:xExitSession = $false
  Function LoadMenuSystem() {
      [INT]$xMenu1 = 0
      [INT]$xMenu2 = 0
      [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUserDeclaredVarsMoreThanAssignments', '', Scope = 'Function')]
      [BOOLEAN]$xValidSelection = $false
      $Loop = $true
      while ( $Loop) {
          Clear-Host
          Write-Host "`n"
          Write-Host "PLEASE SELECT TOOL TO LAUNCH:`n" -ForegroundColor Yellow
          Write-Host "`[1] Tool #1"
          Write-Host "`[2] Tool #2"
          Write-Host "`[3] Tool #3 and sub-options [...]" # Sub-menu
          Write-Host "`[9] Exit"
          try {[int]$xMenu1 = Read-host "`n`Please enter option 1 to 3..."} catch {}
          $ShowMsg = $true                                # Default to show the completed message
          Switch ($xMenu1) {
              1 {
                   Write-Host "Tool #1 launched..." -ForegroundColor Yellow
                }
              2 { 
                   Write-Host "Tool #2 launched..." -ForegroundColor Yellow
                }
              3 {
                   Tool3Menu                            # Show the sub-menu 
                   $ShowMsg = $false                    # Don't display the complete message 
                }
              9 {
                   $Loop = $false
                }
               
              default {
                  Write-Host "`nPlease select one of the options available.`n" -ForegroundColor Red
                  Start-Sleep -Seconds 1
                  $ShowMsg = $false                 # Don't display completed message
              }
          }
          if ($Loop -and $ShowMsg) {
             write-host "`n`n################################## COMPLETED ##################################`n" -ForegroundColor Yellow
             Read-Host "Press any key to go back to option..."
          }
      }
  }
    
  Function Tool3Menu {
      $Loop3 = $True
      while ( $Loop3 ) {
             Clear-Host
             Write-Host "Tool-Sub-Menu:`n" -ForegroundColor Yellow
             Write-Host "`[1] Sub-Tool #1"
             Write-Host "`[2] Sub-Tool #2"
             Write-Host "`[3] Return to main options [...]"
             try {[int]$xMenu2 = Read-host "`n`Please enter option 1 to 3..."} catch {}
             $ShowMsg3 = $True                                  # Default to show the completed message
             Switch ($xMenu2) {
                 1 { 
                      Write-Host "Tool #3 Sub-option #1..." -ForegroundColor Yellow
                   }
                 2 {
                      Write-Host "Tool #3 Sub-option #2..." -ForegroundColor Yellow
                   }
                 3 { 
                      $Loop3 = $False 
                   }
                 default {
                      Write-Host "`nPlease select one of the sub-options available.`n" -ForegroundColor Red
                      Start-Sleep -Seconds 1
                      $ShowMsg3 = $False                 # Don't display completed message 
                 }
             }
             if ($Loop3 -and $ShowMsg3 ) {                       # did user ask to go back
                 write-host "`n`n################################## SUB ITEM COMPLETED ##################################`n" -ForegroundColor Yellow
                 Read-Host "Press any key to return to submenu..."
             }
       }
  }
     
  # Launch main menu
  LoadMenuSystem

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hey @MotoX80 - you saved my script again. Thank you so much.

0 Votes 0 ·