none
How do I use nested Label with nested Switch in Powershell? RRS feed

  • Question

  • 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
        }
    }


    Tirtha Chandan Tripathy


    Thursday, January 29, 2015 6:52 AM

Answers

  • 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


    There's no place like 127.0.0.1

    Thursday, January 29, 2015 1:34 PM
  • 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


    There's no place like 127.0.0.1

    Thursday, January 29, 2015 6:17 PM

All replies

  • 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.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, January 29, 2015 1:15 PM
    Moderator
  • 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


    There's no place like 127.0.0.1

    Thursday, January 29, 2015 1:34 PM
  • 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
    >>


    Tirtha Chandan Tripathy

    Thursday, January 29, 2015 6:00 PM
  • 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
    >>


    Tirtha Chandan Tripathy


    Thursday, January 29, 2015 6:01 PM
  • 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


    There's no place like 127.0.0.1

    Thursday, January 29, 2015 6:17 PM
  • I think I did a typo somewhere while copying and pasting the last time.

    This time worked.

    Thanks Fred


    Tirtha Chandan Tripathy

    Friday, January 30, 2015 1:48 AM