0

I would like to be able to exit the script after using option 1 and Q. R for repeat works and any other key would bring me back to the Menu before. Can i just use another while ($response -eq "Q") ? or is the do while loop just wrong for that? i already tried a if else version but im doing it kinda wrong. Any help please?

 function Show-Menu
{
     param (
           [string]$Title = 'Who? '
     )
     cls
     Write-Host "================ $Title ================"

     Write-Host "1: 1"
     Write-Host "2: 2"
     Write-Host "3: 3"
     Write-Host "4: 4"
     Write-Host "5: 5"
     Write-Host "6: 6"
     Write-Host "7: 7"
     Write-Host "8: 8"

     Write-Host "Q: Q to Quit."
}
do
{
     Show-Menu
     $input = Read-Host "Please choose."
     switch ($input)
     {
           '1' {
                cls
                'You chose option #1'
                do {
                  $response = Read-Host "R to repeat, Q to Quit or anything else to go back" }
                  while ($response -eq "R")
           } '2' {
                cls
                'You chose option #2'
           } '3' {
                cls
                'You chose option #3'
           } 'q' {
                return
           } '3' {
                cls
                'You chose option #1'
           } '4' {
                cls
                'You chose option #2'
           } '' {
                cls
                'You chose option #3'
           } 'q' {
                return
           }
     }
     pause
}
until ($input -eq 'q')
batchn00b
  • 29
  • 7

2 Answers2

1

Just make an if statement. If response contains the Char Q then return.

    function Show-Menu
{
     param (
           [string]$Title = 'Who? '
     )
     cls
     Write-Host "================ $Title ================"

     Write-Host "1: 1"
     Write-Host "2: 2"
     Write-Host "3: 3"
     Write-Host "4: 4"
     Write-Host "5: 5"
     Write-Host "6: 6"
     Write-Host "7: 7"
     Write-Host "8: 8"

     Write-Host "Q: Q to Quit."
}
do
{
     Show-Menu
     $input = Read-Host "Please choose."
     switch ($input)
     {
           '1' {
                cls
                'You chose option #1'
                 do {
                  $response = Read-Host "R to repeat, Q to Quit or anything else to go back" }
                  while ($response -eq "R")
                  if($response.Contains("Q"))
                  {return}

           } '2' {
                cls
                'You chose option #2'
           } '3' {
                cls
                'You chose option #3'
           } 'q' {
                return
           } '3' {
                cls
                'You chose option #1'
           } '4' {
                cls
                'You chose option #2'
           } '' {
                cls
                'You chose option #3'
           } 'q' {
                return
           }
     }
     pause
}
until ($input -eq 'q')

One way to do it

Modro
  • 416
  • 2
  • 14
1

how about:

'1'
{
    cls
    'You chose option #1'
    do
    {
        $response = Read-Host "R to repeat, Q to Quit or anything else to go back" 
    }
    while ($response -eq "R")
    if ($response -eq "q")
    {
        exit
    }
}