Home > Enterprise >  Return to menu after the input result
Return to menu after the input result

Time:04-13

I'm working on a Show-Menu PS script. After the user gives in his input and he gets the result. He should see again the Menu and not "Enter the user ID: "

I know this is possible by removing the Show-Menu but than my user input is not selected for my commands.

Thanks in advance!

function Show-Menu {
    param (
        [string]$Title = 'UserInfo V3.1'
    )

    Write-Host "Enter the user ID: " -ForegroundColor Cyan -NoNewline
    Read-Host 
    Write-Host ""   
    Write-Host "================ $Title ================"   
    Write-Host "Press 'U' for general user info."
    Write-Host "Press 'P' for account and password information."
    Write-Host "Press 'C' for computer info."
    Write-Host "Press 'G' for Virtual Applications (AVC)."
    write-Host "Press 'S' for SNOW info details"
    Write-Host "Press 'Q' to  Quit."
    write-Host "=============================================="
}

do {
    $userName = Show-Menu 
    $Selection = Read-Host "Please make a selection"
    switch ($Selection) {
        'U' { Get-ADUser -Identity $Username; pause }
    }
} until ($Selection -eq 'q')

CodePudding user response:

If Show-Menu is only supposed to show a menu, then prompting a user for input with Read-Host is clearly not appropriate. Move that part out of the Show-Menu function:

function Show-Menu {
    param (
        [string]$Title = 'UserInfo V3.1'
    )

    Write-Host ""   
    Write-Host "================ $Title ================"   
    Write-Host "Press 'U' for general user info."
    Write-Host "Press 'P' for account and password information."
    Write-Host "Press 'C' for computer info."
    Write-Host "Press 'G' for Virtual Applications (AVC)."
    write-Host "Press 'S' for SNOW info details"
    Write-Host "Press 'Q' to  Quit."
    write-Host "=============================================="
}

Write-Host "Enter the user ID: " -ForegroundColor Cyan -NoNewline
$userName = Read-Host 

do {
    Show-Menu 
    $Selection = Read-Host "Please make a selection"
    switch ($Selection) {
        'U' { Get-ADUser -Identity $Username; pause }
    }
} until ($Selection -eq 'q')

CodePudding user response:

[you may find it simpler to just use the Out-Gridview cmdlet. there is a text-only console version available if you can install such.]


the following is a slightly different approach. it accepts a list of possible menu choices and ONLY accepts those items OR x as valid choices.

i think the code is fairly obvious, so i will just show it ... and the onscreen output. [grin]

function Get-MenuChoice
    {
    [CmdletBinding ()]
    Param
        (
        [Parameter (
            Mandatory,
            Position = 0
            )]
            [string[]]
            $MenuList,

        [Parameter (
            Position = 1
            )]
            [string]
            $Title,

        [Parameter (
            Position = 2
            )]
            [string]
            $Prompt = 'Please enter a number from the above list or "x" to exit '
        
        )

    $ValidChoices = 0..$MenuList.GetUpperBound(0)   'x'
    $Choice = ''
    while ([string]::IsNullOrEmpty($Choice))
        {
        Write-Host $Title
        foreach ($Index in 0..$MenuList.GetUpperBound(0))
            {
            Write-Host ('{0} - {1}' -f $Index, $MenuList[$Index])
            }
        $Choice = Read-Host -Prompt $Prompt
        Write-Host ''

        if ($Choice -notin $ValidChoices)
            {
            [System.Console]::Beep(1000, 300)
            Write-Warning ''
            Write-Warning ('    [ {0} ] is not a valid selection.' -f $Choice)
            Write-Warning '    Please try again.'
            Write-Warning ''

            $Choice = ''

            pause
            }
        }

    # send it out to the caller
    if ($Choice -eq 'x')
        {
        'Exit'
        }
        else
        {
        $Choice
        }
    } # end >>> function Get-MenuChoice


'***** demo usage below *****'

$MenuList = @(
    'An Item'
    'Some Other Item'
    'Middle Menu Item'
    'Yet Another Item'
    'The Last Choice'
    )

$Choice = Get-MenuChoice -MenuList $MenuList

'You chose [ {0} ] giving you [ {1} ].' -f $Choice, $MenuList[$Choice]

output for one invalid & one valid input ...

0 - An Item
1 - Some Other Item
2 - Middle Menu Item
3 - Yet Another Item
4 - The Last Choice
Please enter a number from the above list or "x" to exit : 66

WARNING: 
WARNING:     [ 66 ] is not a valid selection.
WARNING:     Please try again.
WARNING: 
Press Enter to continue...: 

0 - An Item
1 - Some Other Item
2 - Middle Menu Item
3 - Yet Another Item
4 - The Last Choice
Please enter a number from the above list or "x" to exit : 1

You chose [ 1 ] giving you [ Some Other Item ].
  • Related