sloppyfrenzy
sloppyfrenzy

Reputation: 271

How do I allow for multiple selections in a numbered menu?

This is related to my previous question about FTP time stamps. I'm working on a script to help make my job, and hopefully others, better by making PC setups easier. As it is right now everything works perfectly, except I'd like to make it more automated. Right now it only allows for one thing at a time. I'd like to be able to choose multiple numbers and have it run them all one after the other.

This needs to work in Windows 7 without importing any extra modules.

Here's what I have so far:

echo ""
$answer = read-host "Select a number"
echo ""

if ($answer -eq 1){uninstall}
if ($answer -eq 2){flashjava}
if ($answer -eq 3){reader}
if ($answer -eq 4){firefox}
if ($answer -eq 5){chrome}
if ($answer -eq 6){office}
if ($answer -eq 7){reader}
if ($answer -eq 8){updates}
if ($answer -eq 9){exit}

else {write-host -ForegroundColor red "Invalid Selection"
    sleep 2
    mainmenu
    }
echo ""

Instead of choosing option 2 (for example), letting that go, then hitting option 3, and so on I'd like to type "2,3" (no quotes) and have it hit both functions one after the other.

I've looked into foreach loops, but can't get it to work.

echo ""
$num = read-host "Select a number"
echo ""

$num.split(",")
ForEach($answer in $num){

if ($answer -eq 1){uninstall}
if ($answer -eq 2){flashjava}
if ($answer -eq 3){reader}
if ($answer -eq 4){firefox}
if ($answer -eq 5){chrome}
if ($answer -eq 6){office}
if ($answer -eq 7){reader}
if ($answer -eq 8){updates}
if ($answer -eq 9){exit}

}

else {write-host -ForegroundColor red "Invalid Selection"
    sleep 2
    mainmenu
    }
echo ""

I'm aware the Else statement would catch anything other than a single 1-9. I've tried commenting that section out, still nothing. It works if I choose a single number, but not multiples. I've also tried putting the numbers in the if statements in quotes. It also displays the number(s) I've chosen before it either runs the function or quits. I'd like to not display any output.

Thanks!

UPDATE: After looking around I found the switch parameter. Seems to work great!

   do {
   do {
    cls
    write-host ""
    write-host "****************************"
    write-host "**       Main Menu        **"
    write-host "****************************"
    write-host ""

    write-host "1 - Uninstall Bundled Apps"
    write-host "2 - Install Flash & Java"
    write-host "3 - Install Adobe Reader"
    write-host "4 - Install Firefox"
    write-host "5 - Install Chrome"
    write-host "6 - Install Office 365"
    write-host "7 - Install All"
    write-host ""
    write-host "8 - Check for Installer Updates"
    write-host ""
    write-host "9 - Exit"

    write-host ""
    $answer = read-host "Select number(s)"

    $ok = $answer -match '[123456789]+$'
    if ( -not $ok) {write-host "Invalid selection"
                    sleep 2
                    write-host ""
                    }
    } until ($ok)

    switch -Regex ( $answer ) {
    "1" {uninstall}
    "2" {flashjava}
    "3" {reader}
    "4" {firefox}
    "5" {chrome}
    "6" {o365}
    "7" {uninstall}
    "8" {updates}
    }

    } until ( $answer -match "9" )

    }

Upvotes: 1

Views: 4047

Answers (1)

briantist
briantist

Reputation: 47832

You were really close actually. The main problem is that $num.Split() returns the array, but does not modify $num, so when you then use $num in your foreach() loop, you are using the original value. Have a look at the change here:

echo ""
$num = read-host "Select a number"
echo ""

$nums = $num.split(",")
ForEach($answer in $nums){

    if ($answer -eq 1){uninstall}
    if ($answer -eq 2){flashjava}
    if ($answer -eq 3){reader}
    if ($answer -eq 4){firefox}
    if ($answer -eq 5){chrome}
    if ($answer -eq 6){office}
    if ($answer -eq 7){reader}
    if ($answer -eq 8){updates}
    if ($answer -eq 9){exit}


}

# else {write-host -ForegroundColor red "Invalid Selection"
#    sleep 2
#    mainmenu
#    }
# commented out because this else no longer corresponds to anything
echo ""

Now just to clean this up a bit, let's look at it with switch:

echo ""
$num = read-host "Select a number"
echo ""

$nums = $num.split(",")
ForEach($answer in $nums){

    switch ($answer) {
        1 {uninstall}
        2 {flashjava}
        3 {reader}
        4 {firefox}
        5 {chrome}
        6 {office}
        7 {reader}
        8 {updates}
        9 {exit}
        default {
            write-host -ForegroundColor red "Invalid Selection"
            sleep 2
            mainmenu
        }
    }
}
echo ""

Upvotes: 1

Related Questions