none
Powershell script with submenus how to go back

    Question

  • So I'm new to scripting and I have a question

    I'm developing a script with a main menu. From the main menu there are sub menus within each other.

    My reason for this script it to choose a PC model, then from there you choose to either install software or drivers.

    My issue is I'm trying to put in an option to go back to the previous menu(s). I have searched through multiple websites and forums and have yet to find a script that I can go back to the previous menu.

    Here's what I have at the moment 

                                                                                                                                                                                                                                                  

    Do
    
    {
    	Write-Host "
    	----------Software/Driver Installation----------
    		1. TOSHIBA 1
    		2. TOSHIBA 2
    		3. TOSHIBA 3
    		4. TOSHIBA 4
    		5. TOSHIBA 5
    		6. TOSHIBA 6
    		7. ACER 1
    		8. ASUS 2
    		9. HP 1
    		10. HP 2
    		11. HP 3
    		12. HP 4
    		13. HP 5
    		14. HP 6
    	------------------------------------------------"
    $Menu = read-host -prompt "Select a number & press enter"
    }	 
    until ($Menu -gt 0 -or $Menu -lt 15)
    cls
    Switch ($Menu) 
    {
    1 {write-Host "
    ----------Software/Driver Installation----------
            Choose Between Software or Driver
                        TOSHIBA R705
    1. Software
    2. Drivers
    3. Go Back
    ------------------------------------------------"
    $sub1 = read-host -prompt "Select a 1 through 3 & press enter"
     ($sub1 -gt 0 -or $sub1 -lt 4)
    
    cls
    Switch ($sub1) 
    {
    1 
    {Write-Host "
    ----------Software Installation----------
    
    1. Package 1
    2. Package 2
    3. Package 3
    4. Package 4
    5. Package 5
    6. Go back to previous menu"
    
    $Input = read-host -prompt "Select a number 1 through 5 and select enter"
    
    switch ($Input)
    {
        1 
            { 
            Write-Host "You chose to install Package 1"
            Invoke-Item '\\network path' 
            }
        2 
            { 
            Write-Host "You chose to install Package 2"
            Invoke-Item '\\network path' 
            }
        3 
           { 
            Write-Host "You chose to install Package 3"
            Invoke-Item '\\network path' 
            }
        4 
           { 
            Write-Host "You chose to install Package 4"
            Invoke-Item '\\network path' 
            }
        5 
            { 
            Write-Host "You chose to install Package 5"
            Invoke-Item '\\network path' 
            }
        6
    
       default 
            {
            Write-Host "The option you have chosen does not exist"  }
    
          }
        2 { }
        3 { }
          }
       }
    }


    Thursday, May 05, 2016 4:21 PM

Answers

  • Here's a rough framework from which you can work. As you'll quickly find out, it gets confusing fast. Take a look at the example and example output, then have fun getting things added and changed.

    Do {
    @'
    
    ----------Software/Driver Installation----------
    1. Toshiba 1
    2. Acer 1
    ------------------------------------------------
    
    '@
    
        $MainMenu = Read-Host -Prompt 'Enter 1 - 2 or Q to quit'
        Switch ($MainMenu) {
            1 {
                Do {
    @'
    
    ---------Software/Driver Installation----------
    1. Software
    2. Drivers
    ------------------------------------------------
    
    '@
                    $1MainMenu = Read-Host -Prompt 'Enter 1 - 2 or B for Back'
                    Switch ($1MainMenu) {
                        '1' {
                                Do {
    @'
    
    --------------Software Installation-------------
    1. Package 1
    2. Package 2
    3. Package 3
    ------------------------------------------------
    
    '@
                                    $1InnerMenu = Read-Host -Prompt 'Enter 1 - 3 or B for Back'
                                    Switch ($1InnerMenu) {
                                     '1' {Write-Output -InputObject '--> You chose to install package 1'; break}
                                     '2' {Write-Output -InputObject '--> You chose to install package 2'; break}
                                     '3' {Write-Output -InputObject '--> You chose to install package 3'}
                                    }
                                } Until ($1InnerMenu -eq 'B')
                            }
                    }
                } Until ($1MainMenu -eq 'B')
            }
        } # End Switch.
    } Until (
        $MainMenu -eq 'Q'
    )

    Edit: Cleaned up errors in code.


    Thursday, May 05, 2016 8:31 PM
  • here is my attempt. feel free to modify it as necessary.

    *edit: you may be able to achieve something similar by using '... | out-gridview -outputmode single' instead of the menu function. and the write-object function can be replaced with a few write-host commands.

    it requires these two functions:
    https://github.com/gangstanthony/PowerShell/blob/master/Menu.ps1
    https://github.com/gangstanthony/PowerShell/blob/master/Write-Object.ps1

    try{ iex $(iwr 'https://raw.githubusercontent.com/gangstanthony/PowerShell/master/Menu.ps1').RawContent -ErrorAction 0 }catch{} try{ iex $(iwr 'https://raw.githubusercontent.com/gangstanthony/PowerShell/master/Write-Object.ps1').RawContent -ErrorAction 0 }catch{} cls 'step1', 'step2', 'step3' | % {rv $_} function step1 { cls $mfrs = @( 'TOSHIBA 1' 'TOSHIBA 2' 'TOSHIBA 3' 'TOSHIBA 4' 'TOSHIBA 5' 'TOSHIBA 6' 'ACER 1' 'ASUS 2' 'HP 1' 'HP 2' 'HP 3' 'HP 4' 'HP 5' 'HP 6' ) $menuparams = @{ Header = '----------Software/Driver Installation----------' Footer = '------------------------------------------------' Object = $mfrs } Menu @menuparams } function step2 ($mfr) { cls $menuparams = @{ Header = "----------Software/Driver Installation----------`r`n`t Choose Between Software or Driver`r`n`t`t`t`t $mfr" Footer = '------------------------------------------------' Object = 'Software', 'Drivers', 'Go Back' }

    Menu @menuparams } function step3 { cls $packages = @( 'Package 1' 'Package 2' 'Package 3' 'Package 4' 'Package 5' 'Go Back' ) $menuparams = @{ Header = '----------Software/Driver Installation----------' Footer = 'Select a number 1 through 6 and select enter' Object = $packages } Menu @menuparams } function step4 ($pkg) { cls Write-Host "You chose to install $pkg" switch ($pkg) { 'Package 1' { Write-Host Invoke-Item '\\network path 1' } 'Package 2' { Write-Host Invoke-Item '\\network path 2' } 'Package 3' { Write-Host Invoke-Item '\\network path 3' } 'Package 4' { Write-Host Invoke-Item '\\network path 4' } 'Package 5' { Write-Host Invoke-Item '\\network path 5' } default { Write-Warning "$pkg does not exist!" } } } ##### script entry point do { if (!$step3 -or $step3 -eq 'Go Back') { do { if (!$step2 -or $step2 -eq 'Go Back') { $step1 = step1 } $step2 = step2 -mfr $step1 } while ($step2 -eq 'Go Back') } $step3 = step3 } while ($step3 -eq 'Go Back') cls $selection = [pscustomobject]@{ MFG = $step1 Install = $step2 Package = $step3 } Write-Object -Header 'You have selected:' -Object $selection $continue = Menu -Header '----------Software/Driver Installation----------' -Footer '----- Continue? -----' -Object 'Yes', 'No' if ($continue.ToLower()[0] -eq 'n') { return } step4 $selection.Package



    [>_]

    Thursday, May 05, 2016 9:30 PM

All replies

  • Here's a rough framework from which you can work. As you'll quickly find out, it gets confusing fast. Take a look at the example and example output, then have fun getting things added and changed.

    Do {
    @'
    
    ----------Software/Driver Installation----------
    1. Toshiba 1
    2. Acer 1
    ------------------------------------------------
    
    '@
    
        $MainMenu = Read-Host -Prompt 'Enter 1 - 2 or Q to quit'
        Switch ($MainMenu) {
            1 {
                Do {
    @'
    
    ---------Software/Driver Installation----------
    1. Software
    2. Drivers
    ------------------------------------------------
    
    '@
                    $1MainMenu = Read-Host -Prompt 'Enter 1 - 2 or B for Back'
                    Switch ($1MainMenu) {
                        '1' {
                                Do {
    @'
    
    --------------Software Installation-------------
    1. Package 1
    2. Package 2
    3. Package 3
    ------------------------------------------------
    
    '@
                                    $1InnerMenu = Read-Host -Prompt 'Enter 1 - 3 or B for Back'
                                    Switch ($1InnerMenu) {
                                     '1' {Write-Output -InputObject '--> You chose to install package 1'; break}
                                     '2' {Write-Output -InputObject '--> You chose to install package 2'; break}
                                     '3' {Write-Output -InputObject '--> You chose to install package 3'}
                                    }
                                } Until ($1InnerMenu -eq 'B')
                            }
                    }
                } Until ($1MainMenu -eq 'B')
            }
        } # End Switch.
    } Until (
        $MainMenu -eq 'Q'
    )

    Edit: Cleaned up errors in code.


    Thursday, May 05, 2016 8:31 PM
  • here is my attempt. feel free to modify it as necessary.

    *edit: you may be able to achieve something similar by using '... | out-gridview -outputmode single' instead of the menu function. and the write-object function can be replaced with a few write-host commands.

    it requires these two functions:
    https://github.com/gangstanthony/PowerShell/blob/master/Menu.ps1
    https://github.com/gangstanthony/PowerShell/blob/master/Write-Object.ps1

    try{ iex $(iwr 'https://raw.githubusercontent.com/gangstanthony/PowerShell/master/Menu.ps1').RawContent -ErrorAction 0 }catch{} try{ iex $(iwr 'https://raw.githubusercontent.com/gangstanthony/PowerShell/master/Write-Object.ps1').RawContent -ErrorAction 0 }catch{} cls 'step1', 'step2', 'step3' | % {rv $_} function step1 { cls $mfrs = @( 'TOSHIBA 1' 'TOSHIBA 2' 'TOSHIBA 3' 'TOSHIBA 4' 'TOSHIBA 5' 'TOSHIBA 6' 'ACER 1' 'ASUS 2' 'HP 1' 'HP 2' 'HP 3' 'HP 4' 'HP 5' 'HP 6' ) $menuparams = @{ Header = '----------Software/Driver Installation----------' Footer = '------------------------------------------------' Object = $mfrs } Menu @menuparams } function step2 ($mfr) { cls $menuparams = @{ Header = "----------Software/Driver Installation----------`r`n`t Choose Between Software or Driver`r`n`t`t`t`t $mfr" Footer = '------------------------------------------------' Object = 'Software', 'Drivers', 'Go Back' }

    Menu @menuparams } function step3 { cls $packages = @( 'Package 1' 'Package 2' 'Package 3' 'Package 4' 'Package 5' 'Go Back' ) $menuparams = @{ Header = '----------Software/Driver Installation----------' Footer = 'Select a number 1 through 6 and select enter' Object = $packages } Menu @menuparams } function step4 ($pkg) { cls Write-Host "You chose to install $pkg" switch ($pkg) { 'Package 1' { Write-Host Invoke-Item '\\network path 1' } 'Package 2' { Write-Host Invoke-Item '\\network path 2' } 'Package 3' { Write-Host Invoke-Item '\\network path 3' } 'Package 4' { Write-Host Invoke-Item '\\network path 4' } 'Package 5' { Write-Host Invoke-Item '\\network path 5' } default { Write-Warning "$pkg does not exist!" } } } ##### script entry point do { if (!$step3 -or $step3 -eq 'Go Back') { do { if (!$step2 -or $step2 -eq 'Go Back') { $step1 = step1 } $step2 = step2 -mfr $step1 } while ($step2 -eq 'Go Back') } $step3 = step3 } while ($step3 -eq 'Go Back') cls $selection = [pscustomobject]@{ MFG = $step1 Install = $step2 Package = $step3 } Write-Object -Header 'You have selected:' -Object $selection $continue = Menu -Header '----------Software/Driver Installation----------' -Footer '----- Continue? -----' -Object 'Yes', 'No' if ($continue.ToLower()[0] -eq 'n') { return } step4 $selection.Package



    [>_]

    Thursday, May 05, 2016 9:30 PM
  • Thank you for your input!

    I'm going to give it a try to see how it works and let you know.

    Friday, May 06, 2016 1:41 PM
  • Hi,

    May I know if there's any update on this case? Are you able to set the menu go back function according to the above scripts?

    As we haven't heared from you for a while, I'd mark the helpful post as answers, feel free to post us back if you have any concern.

    Best Regards,

    Elaine


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, May 12, 2016 8:56 AM
    Moderator