I am wondering if it is possible to create switch cases with more than 1 value per case. I am trying to take in user input in different ways.
The menu looks something like:
$database = Read-Host "[1] db 1 [2] db2 [3] db3 [4] ALL dbs"
This is then passed as a parameter into 4 different functions, based off of what the user response is in my next Read-host.
Without making a ridiculous amount of cases, I want to be able to accept all possible inputs. Ie, if the user wants to see 2 databases, they can enter 32 or 23. If they want to see all, it would accept 4, 321, 123, 213… Currently I am trying to accomplish this by:
switch ($database) {
(4 -or 123 -or 312 -or ...) {#return all paths}
(12 -or 21) { #return 1st 2 db paths}
(31 -or 13) { #return 1st and last db paths}
(1) {#return 1st db path}
...
}
This is causing a syntax error, so it clearly does not work.
As you can tell, there are going to be a lot of cases already (7). If I had to list out every single option there would be more than double (16). Is there a way to create a one to many association in a switch case? If not, does anyone have an idea of a new approach I can take to this problem?
SOLUTION
switch -regex ($database)
{
"[*1*]" {"\\db1"}
"[*2*]" {"\\db2"}
"[*3*]" {"\\db3"}
"[4]" {"\\db1";"\\db2";"\\db3"}
default {Write-Host "Database(s) were not properly specified,
Please run the script again"
exit}
}
This accomplishes what I was going for, although I was never able to figure out how to put a conditional -or in a switch statement, I do not believe it is possible
I think that you can work on
-have a look here