Mark Minasi's Reader Forum
Mark Minasi's Reader Forum
Home | Profile | Register | Active Topics | Active Polls | Members | Search | FAQ | Minasi Forum RSS Feed
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 HALP! Questions on Windows and Windows Server
 PowerShell
 Nested menu
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Rambler
Major Contributor

Czech Republic
949 Posts
Status: offline

Posted - 06/27/2012 :  2:05:03 PM  Show Profile  Reply with Quote
Hi, I've found this neat piece of code from Jeffery and I wanted to make nested menus using this.

http://jdhitsolutions.com/blog/2011/12/friday-fun-a-powershell-console-menu/

The only problem is that in that sample code when user presses Q, the script ends, but in the nested menus, I just need it to leave the current loop. I've tried using Break (in the code below), but that doesn't do anything there. Exit/Return exits the whole script.

Any ideas?

Nested menu might look like this:
$menu1=@"
1 Item1
2 Item2
Q Quit
"@

$menu2=@"
1 Add
2 Remove
0 Exit to previous menu
"@


#Keep looping and running the menu until the user selects Q (or q).
Do {
    #use a Switch construct to take action depending on what menu choice is selected.
    Switch (Show-Menu $menu1 "Menu1" -clear) {
     "1" {
         Do {
             Switch (Show-menu $menu2 "Menu2" -clear) {
             "1" {
                 }
             "2" {
                 }
             "0" {Break}
            } #switch
          } #do
         While ($true)
         } #menu    
     "2" {}
     "Q" {Write-Host "End" -ForegroundColor Cyan
         Return
         }
     Default {Write-Warning "Wrong choice, try again"
              sleep -seconds 1}
    } #switch
} While ($True)

Edited by - Rambler on 06/27/2012 2:05:31 PM

Nobody
Here To Stay

USA
184 Posts
Status: offline

Posted - 06/27/2012 :  9:01:48 PM  Show Profile  Visit Nobody's Homepage  Reply with Quote
I'm curious to see Jeffery's answer, but in the mean time, this will work.


#Keep looping and running the menu until the user selects Q (or q).
Do {
    #use a Switch construct to take action depending on what menu choice is selected.
    $back = 1
    Switch (Show-Menu $menu1 "Menu1" -clear) {
     "1" {
         Do {
             Switch (Show-menu $menu2 "Menu2" -clear) {
             "1" {
                 }
             "2" {
                 }
             "0" {$back = 0}
            } #switch
          } #do
         While ($back)
         } #menu    
     "2" {}
     "Q" {Write-Host "End" -ForegroundColor Cyan
         Return
         }
     Default {Write-Warning "Wrong choice, try again"
              sleep -seconds 1}
    } #switch
} While ($True)

aka - Matt
www.SnowTrek.org

Edited by - Nobody on 06/27/2012 9:07:00 PM
Go to Top of Page

Rambler
Major Contributor

Czech Republic
949 Posts
Status: offline

Posted - 06/28/2012 :  02:08:30 AM  Show Profile  Reply with Quote
Thanks, that was trivial, I don't know why I didn't think of that.
Go to Top of Page

jhicks
Here To Stay

USA
283 Posts
Status: offline

Posted - 06/28/2012 :  08:30:05 AM  Show Profile  Visit jhicks's Homepage  Reply with Quote
I felt my ears burning when I saw this thread in my email. The Do loop is the way I would do it.

Jeffery Hicks
Windows PowerShell MVP

http://jdhitsolutions.com.blog
http://twitter.com/JeffHicks
http://www.ScriptingGeek.com
Now Available: Managing Active Directory with Windows PowerShell: TFM 2nd ed.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
Mark Minasi's Reader Forum © 2002-2011 Mark Minasi Go To Top Of Page
This page was generated in 0.08 seconds. Snitz Forums 2000