Answer the question
In order to leave comments, you need to log in
Powershell. How to use switch on two variables?
Good afternoon.
Can you please tell me if it is possible to use switch for two variables? Instead of if
I have variables in two functions. In one $a in another $b. They take values either true or false according to body movements in functions.
In the third function, I take actions based on the values of $a and $b
I would like to understand if switch can be used instead of if here?
Conditions to be handled:
1. $a=true and $b=true
2. $a=false and $b=false
3. 1. $a=false and $b=true
1. $a=true and $b=false
thanks in advance
Answer the question
In order to leave comments, you need to log in
switch([System.Tuple]::Create($true, $false))
{
([System.Tuple]::Create($true, $true)) {"1"}
([System.Tuple]::Create($true, $false)) {"2"}
}
Good afternoon!
As an option, give the sum of values to the Switch input (example below), but it will be necessary to modify the variables at the output of the first two functions in order to exclude the coincidence of the results of their sum
$a = 0
$b = 0
switch ($a+$b){
0 {Write -Host "0";break}
1 {Write-Host "1";break}
2 {Write-Host "2";break}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question