I would like to create a script in powershell, that does several things depending on which parameters is set or not. My code looks like the following:
Param(
[parameter(Mandatory=$true,Position=1)]
[String]$var1 ,
[parameter(Mandatory=$false,Position=2)]
[int]$var2 ,
[parameter(Mandatory=$false,Position=3)]
[int]$var3 ,
[parameter(Mandatory=$false,Position=4)]
[string]$var4
)
....
And then i want to either do a if-elseif to check which parameters is set:
if (!$var2 -and !$var3 -and !$var4) {
... Do something with $var1
} elseif (!$var2 -and !$var3 -and $var4) {
... Do something with $var1 and $var4
} else {
throw an error
}
.. or do a switch case if that is more suitable. I cant decide whether or not it is. Can anyone come up with a small example to do this? Thanks in advance. And Thank you for taking your time to help me out.
Switch
is more elegant and easier to read imo. You can also use$PSBoundParameters
too.switch
with multiplereturn
statements in addition to Mathias's helpful answer.