How to pass a switch parameter to another PowerShell script? How to pass a switch parameter to another PowerShell script? powershell powershell

How to pass a switch parameter to another PowerShell script?


You can specify $true or $false on a switch using the colon-syntax:

compile-tool1.ps1 -VHDL2008:$truecompile-tool1.ps1 -VHDL2008:$false

So just pass the actual value:

compile-tool1.ps1 -VHDL2008:$VHDL2008


Try

compile-tool1.ps1 -VHDL2008:$VHDL2008.IsPresent 


Another solution. If you declare your parameter with a default value of $false:

[switch] $VHDL2008 = $false

Then the following (the -VHDL2008 option with no value) will set $VHDL2008 to $true:

compile-tool1.ps1 -VHDL2008

If instead you omit the -VHDL2008 option, then this forces $VHDL2008 to use the default $false value:

compile-tool1.ps1

These examples are useful when calling a Powershell script from a bat script, as it is tricky to pass a $true/$false bool from bat to Powershell, because the bat will try to convert the bool to a string, resulting in the error:

Cannot process argument transformation on parameter 'VHDL2008'. Cannot convert value "System.String" to type "System.Management.Automation.SwitchParameter". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0.