Powershell passing switch parameter to self
powershell
Solution
Presumably call of `powershell.exe` is not needed at all. Replace it with the operator `&`, i.e. call the script in the same session and avoid parameter transformations and related issues. The issues are resolvable but better be avoided in the first place. That is, do
& $currentDirectory/Deploy.ps1 -reload:$true -env:$env -provisionsites:$provisionsites
As for the issues. `$provisionsites` is converted to a string (`True` or `False`, not `$true` or `$false`) before passing in the external application. Thus, the result actual arguments look like `-provisionsites:True`. Then in a new powershell session it passes such an argument to the script `Deploy.ps1`. It fails because strings `True` or `False` are not expected, a Boolean value is expected.
A possible workaround would be adding escaped `$`
powershell $currentDirectory/Deploy.ps1 ... -provisionsites:`$$provisionsites
But consider to remove the call of `powershell` and its issues.
Problem
I have this powershell script that calls itself (because stage 1 of the script is to load assemblies in the GAC so i need AppDomain refreshed). How do I pass a switch parameter to self. At the moment I am doing this: ``` if ($provisionsites -eq $true) { powershell $currentDirectory/Deploy.ps1 -reload:$true -env:$env -provisionsites } else { powershell $currentDirectory/Deploy.ps1 -reload:$true -env:$env } ``` It seems too verbose to me. If i try this: ``` powershell $currentDirectory/Deploy.ps1 -reload:$true -env:$env -provisionsites:$provisionsites ``` It fails with : Cannot convert value "System.String" to type "System.Management.Automat ion.SwitchParameter