Powershell interactive script menu. I can’t quit(invalid option)

Asked

Viewed 312 times

1

I’m a beginner in Powershell and I have a little problem.

I have 2 scripts (menu and submenu). I run script_A (menu) and when I select option 9 (Exit) it works as expected. But when I run script_A and choose option 1 (submenu), then choose option 9 (return to script_A) and again select 9 (Exit), this message appears: invalid option.

Why? Can anyone help me? A and B scripts are:

Script_a:

$resposta = $null
$vocetemcerteza = $null

$PSScriptRoot
$ScriptToRun1= $PSScriptRoot+"\DNSMenu.ps1"

function vocetemcerteza {
      $vocetemcerteza = read-host "Do you have exit? (y/n)"  
         if ($vocetemcerteza -eq "y"){Exit}  
         elseif ($vocetemcerteza -eq "n"){mainmenu}  
         else {write-host -foregroundcolor red "Invalid Option."
            vocetemcerteza  
           }  
       }  


function mainmenu{  
$vocetemcerteza = "n"
cls
echo "    1. DNS"  
echo ""  
echo "    9. Exit" 

$resposta = read-host "Choose a option:"  
if ($resposta -eq 1){&$ScriptToRun1}  
if ($resposta -eq 9){vocetemcerteza} 
else {echo ""
   write-host -ForegroundColor magenta "Invalid Option."  
   sleep 1
   cls  
   mainmenu
   }  
   }  
 mainmenu

Script_b:

$ScriptToRun1= $PSScriptRoot+"\DNSemLoteMenu.ps1"

function mainmenu{  
$vocetemcerteza = "n"
cls
echo "" 
echo "    1. Create register" 
echo ""  

$resposta = read-host "Choose a option:"  
   if ($resposta -eq 1){&$ScriptToRun1}  
   if ($resposta -eq 9){mainmenu} 
   else {echo ""
      write-host -ForegroundColor magenta "Invalid Option."  
      sleep 1
      mainmenu  
      }  
    }  
 mainmenu
  • Just a hint, your menu should be inside a Switch, check the example of the link.... https://technet.microsoft.com/en-us/library/ff730937.aspx

1 answer

1


Browser other questions tagged

You are not signed in. Login or sign up in order to post.