Reputation: 24470
In the below example I've got an IF
statement to determine which parameters send to Demo
when called recursively. If $y
were a boolean value rather than a switch I could simply call Demo -x $x -y $y
; but as a switch that's not a viable option.
function Demo {
[CmdletBinding()]
param (
[int]$x
,[switch]$y
)
process {
$x--
if ($x -gt 0) {
"$x - $($y.IsPresent)"
if($y.IsPresent) {
Demo -x $x -y
} else {
Demo -x $x
}
}
}
}
Demo 10 -y
Demo 10
Question
Is the above the correct way to handle this scenario, or does a cleaner option exist?
Upvotes: 3
Views: 360
Reputation: 47862
You can force a switch parameter by calling it like this: -Switch:$true
(redundant in most cases) or -Switch:$false
so for your example:
Demo -y:$y
By the way in this example, you could also use splatting:
Demo @PSBoundParameters
But your post is clearly a MVCE so this may not apply to what you're actually doing, or may need modification, especially if you have default values for some parameters (full disclosure: my blog).
Upvotes: 5