Powershell interactive script menu. I can not get out (invalid option)

1

I am a beginner in Powershell and I have a small 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 I choose option 1 (submenu), and then I choose option 9 (return to script_A) and again select 9 (exit), this message appears: invalid option.

Why? Can someone help me? scripts A and B 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
    
asked by anonymous 11.08.2017 / 03:35

1 answer

0

It was a small problem in line 7 of Script_A.

In the function, in the 3rd line: when we want to leave a function the correct command is "break". As I was using the "exit", I gave the error message.

I got the question in another post from here: link

Problem solved.

    
23.08.2017 / 03:06