none
Back to menu after selection. RRS feed

  • Question

  • Hello,

    This must be very simple, but im not be able to get it.

    Function Show-Menu
    {
        param (
            [string]$Title = 'Script Options'
        )
        Clear-Host
    	Write-Host "============== $Title =============="
    	Write-Host " 1 "
    	Write-Host " 2 "
    	Write-Host " 3 "
        Write-Host " 4 "
        Write-Host " 0 > Press '0' to cancel "
    	}
    Show-Menu
    $selection = Read-Host "Please make a selection"
    
    		
    Function1
    Function2
    Function3
    Function4
    
    #Goodbye Message Function
    Function exit {
    Return
    }
    
    switch ($selection) {
        1 {Function1}		
    		
    	2 {Function2}
    	
    	3 {Function3}
    		
    	4 {Function4}
    			
    	0 {Exit}
    }

    I just  need back to menu when the selected Function (1,2,3 or 4) ends.

    I try call again function menu with "Show-menu" after the last } but it doesnt work.

    Thanks



    • Edited by Pedro_A Thursday, February 28, 2019 8:52 AM
    Thursday, February 28, 2019 8:50 AM

Answers

  • function Function1 { }
    function Function2 { }
    function Function3 { }
    function Function4 { }
    
    function Show-Menu {
        param (
        [string]$Title = 'Script Options'
        )
        While (1) {
            Clear-Host
            Write-Host '============== $Title =============='
            Write-Host ' 1 '
            Write-Host ' 2 '
            Write-Host ' 3 '
            Write-Host ' 4 '
            Write-Host ' 0 > Press '0' to cancel '
            [int]$ans = Read-Host 'Please make a selection'
            switch ($ans) {
                1 { Function1 }
                2 { Function2 }
                3 { Function3 }
                4 { Function4 }
                0 { return }
                default {}
            }
        }
    }
    
    Show-Menu


    \_(ツ)_/

    • Proposed as answer by I.T Delinquent Thursday, February 28, 2019 11:50 AM
    • Marked as answer by Pedro_A Thursday, February 28, 2019 12:54 PM
    Thursday, February 28, 2019 11:34 AM

All replies

  • It won't work <g class="gr_ gr_25 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" data-gr-id="25" id="25">because</g> your declaring functions inside of functions. Try something like this:

    Function Show-Menu { param ( [string]$Title = 'Script Options' ) Clear-Host Write-Host "============== $Title ==============" Write-Host " 1 " Write-Host " 2 " Write-Host " 3 " Write-Host " 4 " Write-Host " 0 > Press '0' to cancel " }

    Show-Menu

    $selection = Read-Host "Please make a selection"

    switch ($selection){

    '1' {do-something}

    '0' {Exit}

    }


    Thursday, February 28, 2019 9:40 AM
  • You can declare a function in a function but it needs to be defined first.  You cannot use "exit" as a function name as it is a reserved word in PowerShell.


    \_(ツ)_/

    Thursday, February 28, 2019 9:57 AM
  • Where im declaring function inside function ?
    Thursday, February 28, 2019 10:43 AM
  • Exit is just a example, im not using it really (exit word)
    • Edited by Pedro_A Thursday, February 28, 2019 10:52 AM
    Thursday, February 28, 2019 10:44 AM
  • If you would format and indent your code correctly it would be easier to see that and to understand the example and the question.

    This is what it should like:

    function Function1{}
    function Function2{}
    function Function3{}
    function Function4{}
    
    #Goodbye Message Function
    function exitMenu {
        Return
    }
    
    
    function Show-Menu {
        param (
            [string]$Title = 'Script Options'
        )
        Clear-Host
        Write-Host "============== $Title =============="
        Write-Host " 1 "
        Write-Host " 2 "
        Write-Host " 3 "
        Write-Host " 4 "
        Write-Host " 0 > Press '0' to cancel "
    }
    
    Show-Menu
    $selection = Read-Host "Please make a selection"
    
    switch ($selection) {
        1 { Function1 }
        2 { Function2 }
        3 { Function3 }
        4 { Function4 }
        0 { exitMenu }
    }
    

    Now ask your question.


    \_(ツ)_/

    Thursday, February 28, 2019 10:56 AM
  • Is this what you are asking?

    function Function1{}
    function Function2{}
    function Function3{}
    function Function4{}
    
    function Show-Menu {
        param (
            [string]$Title = 'Script Options'
        )
        While(1){
            Clear-Host
            Write-Host '============== $Title =============='
            Write-Host ' 1 '
            Write-Host ' 2 '
            Write-Host ' 3 '
            Write-Host ' 4 '
            Write-Host ' 0 > Press '0' to cancel '
            [int]$ans = Read-Host 'Please make a selection' 
            switch ($ans) {
                1 { Function1 }
                2 { Function2 }
                3 { Function3 }
                4 { Function4 }
                0 { return }
            }
        }
    }
    
    Show-Menu
    


    \_(ツ)_/

    Thursday, February 28, 2019 10:59 AM
  • I copied your estructure, and works correctly. 

    Now, im in the same place. 

    I try to back menu after functions execution

    3 { Function3 } 4 { Function4 } 0 { exitMenu } }

    Show-Menu

    But doenst works. 

    My question is, why ? im calling a function that works before, why now doesnt work ?

    Thursday, February 28, 2019 11:10 AM
  • function Function1 { }
    function Function2 { }
    function Function3 { }
    function Function4 { }
    
    function Show-Menu {
        param (
        [string]$Title = 'Script Options'
        )
        While (1) {
            Clear-Host
            Write-Host '============== $Title =============='
            Write-Host ' 1 '
            Write-Host ' 2 '
            Write-Host ' 3 '
            Write-Host ' 4 '
            Write-Host ' 0 > Press '0' to cancel '
            [int]$ans = Read-Host 'Please make a selection'
            switch ($ans) {
                1 { Function1 }
                2 { Function2 }
                3 { Function3 }
                4 { Function4 }
                0 { return }
                default {}
            }
        }
    }
    
    Show-Menu


    \_(ツ)_/

    • Proposed as answer by I.T Delinquent Thursday, February 28, 2019 11:50 AM
    • Marked as answer by Pedro_A Thursday, February 28, 2019 12:54 PM
    Thursday, February 28, 2019 11:34 AM
  • Ok, now i understand.

    Many thanks ;)

    Thursday, February 28, 2019 12:52 PM