How do I use nested Label with nested Switch in Powershell?

Hi,

I am trying to run the script with Label and Switch in Menu format so that the main module calls the submodule and ends in the submodule. The submodule should be able to call another module.

Please correct the script, as I think I am going wrong

Do { Write-Host " ----------MENU A----------
1 = option 1
2 = Option 2
3 = Option 3
---------------------------"
$choice1 = read-host -prompt "Select number & press enter" }
until ($choice1 -eq "1" -or $choice1 -eq "2" -or $choice1 -eq "3")
Switch ($choice1)
    { "1" break :module1
      "2" break :module2
      "3" break :module3
    }
    
module1
    {
    Do { Write-Host " ----------MENU B----------
    1 = option 1
    2 = Option 2
    ---------------------------"
    $choice2 = read-host -prompt "Select number & press enter" }
    until ($choice2 -eq "1" -or $choice2 -eq "2")
    Switch ($choice2)
    { write "Module 2"
      "1" break :module2
      "2" break :module3
    }
}

module2
    {
    Do { Write-Host " ----------MENU C----------
    1 = option 1
    ---------------------------"
    $choice3 = read-host -prompt "Select number & press enter" }
    until ($choice3 -eq "1")
    Switch ($choice3)
    { write "Module 3"
      "1" break :module2
    }

January 29th, 2015 9:52am

You're misunderstanding Break.  

It's not a GoTo, it can only be used to exit to a parent loop from inside a nested loop, or when used in a Switch, to exit the Switch block:

about_Break

You can't use it to jump forward in your script, or to go back to a previous point, outside of a nested loop construct.

Free Windows Admin Tool Kit Click here and download it now
January 29th, 2015 4:15pm

Hi Tirtha,

What you want to do in PowerShell is separate elements of logic trees into individual functions. I rewrote your script attempt somewhat to show you how to do this:

function Show-MenuMain
{
	Do
	{
		Write-Host @"
----------MENU A---------- 
1 = option 1 
2 = Option 2 
3 = Option 3 
---------------------------
"@
		$Choice = read-host -prompt "Select number & press enter"
	}
	until ($Choice -eq "1" -or $Choice -eq "2" -or $Choice -eq "3")
	Switch ($Choice)
	{
		"1" { Show-MenuModule1 }
		"2" { Show-MenuModule2 }
		"3" { Show-MenuModule3 }
	}
}

function Show-MenuModule1
{
	Do
	{
		Write-Host @"
----------MENU B---------- 
1 = option 1 
2 = Option 2 
---------------------------
"@
		$Choice = read-host -prompt "Select number & press enter"
	}
	until ($Choice -eq "1" -or $Choice -eq "2")
	Switch ($Choice)
	{
		"1" { Show-MenuModule2 }
		"2" { Show-MenuModule3 }
	}
}

function Show-MenuModule2
{
	Do
	{
		Write-Host @"
----------MENU C---------- 
1 = option 1 
---------------------------
"@
		$Choice = read-host -prompt "Select number & press enter"
	}
	until ($Choice -eq "1")
	Switch ($Choice)
	{
		"1" { Show-MenuModule1 }
	}
}

Show-MenuMain

I know, this really only prepares the path to a host of new questions, more than answering your current ones, but it gives you something to work with and build upon.

Cheers,
Fred

January 29th, 2015 4:34pm

Tried to run with fscript; did not work; so tried the mainmenu first and you can see there are no results.Is there any change required on the script

PS C:\>     function Show-MenuMain
>>     {
>>     Do
>>     {
>>     Write-Host @"
>>     ----------MENU A----------
>>     1 = option 1
>>     2 = Option 2
>>     3 = Option 3
>>     ---------------------------
>>     "@
>>     $Choice = read-host -prompt "Select number & press enter"
>>     }
>>     until ($Choice -eq "1" -or $Choice -eq "2" -or $Choice -eq "3")
>>     Switch ($Choice)
>>     {
>>     "1" { Show-MenuModule1 }
>>     }
>>     }
>>
>> Show-MenuMain
>>

Free Windows Admin Tool Kit Click here and download it now
January 29th, 2015 9:00pm

Tried to run with script; did not work; so tried the mainmenu first and you can see there are no results.Is there any change required on the script

PS C:\>     function Show-MenuMain
>>     {
>>     Do
>>     {
>>     Write-Host @"
>>     ----------MENU A----------
>>     1 = option 1
>>     2 = Option 2
>>     3 = Option 3
>>     ---------------------------
>>     "@
>>     $Choice = read-host -prompt "Select number & press enter"
>>     }
>>     until ($Choice -eq "1" -or $Choice -eq "2" -or $Choice -eq "3")
>>     Switch ($Choice)
>>     {
>>     "1" { Show-MenuModule1 }
>>     }
>>     }
>>
>> Show-MenuMain
&g

January 29th, 2015 9:01pm

Hi Tirtha,

this is a powershell script, so ... basically, paste it into a .ps1 file (Example: "C:\Temp\ExampleScript.ps1"; Just create a .txt file and rename it). Then rightklick on it and select "run with PowerShell" - a console window should now appear, display the first menu and prompt you for your input.

Cheers,
Fred

Free Windows Admin Tool Kit Click here and download it now
January 29th, 2015 9:17pm

I think I did a typo somewhere while copying and pasting the last time.

This time worked.

Thanks Fred

January 30th, 2015 4:48am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics