Tool Launcher Menu in PowerShell

akosijesyang 101 Reputation points
2021-10-16T00:37:53.127+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 32,061 Reputation points
    2021-10-16T14:54:54.207+00:00

    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
    

0 additional answers

Sort by: Most helpful