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:
Then went to the Sub-menu:
Selected Sub-Tool #1:
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...":
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
}