Joseph Sanders
Joseph Sanders

Reputation: 143

Using Powershell, Display custom message if user input is not a number or is not one of the given options

I am trying to do the following:

Display a list of options for a user to input. All user input choices are integers (in this case 1, 2, 3, 4, and 5) If the user inputs one of the valid options, it should echo their input on the screen. If the user input is greater than the options given, it should give the user a message that their input was greater than the allowed options. If the user input is lower than the options given, it should give the user a message that their input was lower than the allowed options. If the user input is not an integer, it should give them a message saying their input was not a valid number. If the user input is Empty, it should display a message saying their input cannot be blank. Then of course, in case there is something else that goes wrong that is not defined above, it should tell them there was an unknown error.

Here is what I have, and it mostly works....

cls
echo "What computers are we working on?"
echo ""
echo "1 = All Computer Lab Computers"
echo "2 = Watts' Computer Lab Computers"
echo "3 = Wells' Computer Lab Computers"
echo "4 = A Specific Computer"
echo "5 = Custom list of Computer Lab Computers"
echo ""
echo ""
$empty = $null
$result = $null
[int]$result = read-host "Make a Selection (1 - 5) "
cls
Clear-Host
IF ( $result -lt "6" -and $result -gt "0" ) { echo " You Selected $result" }
ElseIf ( $result -ge "6") {echo "Your Selection of $result was greater than 
allowed options" }
ElseIf ( $result -le "0" ) {echo "Your Selection of $result was smaller than 
allowed options" }
ElseIf ( $result -eq "") {echo "Your input cannot be blank!" }
ElseIf ( $result -isNot ([int]) ) {echo " Your Selection of $result is not a 
valid number. Please try again."}
Else { echo "No clue what happened...but you chose $result"}

However, if the user inputs a non integer or leaves the input blank, it does not display the intended message, and instead displays the message for the user input being lower than the allowed options. It also is returning the users input as "0" even if they do not put anything in

Any ideas on how I can make this work as intended?

Upvotes: 1

Views: 803

Answers (2)

Lee_Dailey
Lee_Dailey

Reputation: 7489

here's yet another way to do a text menu ... [grin]

$MenuList = @(
    'All Computer Lab Computers'
    'Watts Computer Lab Computers'
    'Wells Computer Lab Computers'
    'A Specific Computer'
    'Custom list of Computer Lab Computers'
    )

$MenuTitle = 'What computers are we working on?'

$ValidChoices = 1..($MenuList.Count)

$Choice = ''
while ([string]::IsNullOrEmpty($Choice))
    {
    Clear-Host
    Write-Host $MenuTitle
    foreach ($ML_Item in $MenuList)
        {
        Write-Host ('    {0} - {1}' -f ($MenuList.IndexOf($ML_Item) + 1), $ML_Item)
        }
    $Choice = Read-Host 'Please select an item from the above list by number or [ x ] to exit '
    Write-Host ''

    if ($Choice -eq 'x')
        {
        Write-Host 'Exiting now ...'
        break
        }
    if ($Choice -notin $ValidChoices)
        {
        [console]::Beep(1000, 300)
        Write-Warning ('    Your selection [ {0} ] is not valid.' -f $Choice)
        Write-Warning '    Please try again ...'
        pause
        $Choice = ''
        }
    }

''
'You chose [ {0} ]' -f $Choice

the menu display ...

What computers are we working on?
    1 - All Computer Lab Computers
    2 - Watts Computer Lab Computers
    3 - Wells Computer Lab Computers
    4 - A Specific Computer
    5 - Custom list of Computer Lab Computers
Please select an item from the above list by number or [ x ] to exit : 

output for invalid input ...

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

output for valid input of 2 ...

You chose [ 2 ]

Upvotes: 1

Gridonyx
Gridonyx

Reputation: 191

You can make this a lot more streamlined using do/until and try/catch, this will eliminate the need for all the if statements as well

cls
$menu = "What computers are we working on?
1 = All Computer Lab Computers
2 = Watts' Computer Lab Computers
3 = Wells' Computer Lab Computers
4 = A Specific Computer
5 = Custom list of Computer Lab Computers

"
echo $menu
$result = $null

do {
    try {
        $isValid = $true
        [int]$result = read-host "Make a Selection (1 - 5) "
    }
    catch {$isValid = $false}
        Clear-Host
        echo "Your Selection of $result is not a valid number. Please try again"
        echo ""
        echo $menu
    } # end do 
until (($result -lt 6 -and $result -gt 0) -and $isValid)

cls
Clear-Host
echo " You Selected $result"

Upvotes: 0

Related Questions