I have to invoke a PowerShell script from a batch file. One of the arguments to the script is a boolean value:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -File .\RunScript.ps1 -Turn 1 -Unify $false
The command fails with the following error:
Cannot process argument transformation on parameter 'Unify'. Cannot convert value "System.String" to type "System.Boolean", parameters of this type only accept booleans or numbers, use $true, $false, 1 or 0 instead.
At line:0 char:1
+ <<<< <br/>
+ CategoryInfo : InvalidData: (:) [RunScript.ps1], ParentContainsErrorRecordException <br/>
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,RunScript.ps1
As of now I am using a string to boolean conversion inside my script. But how can I pass boolean arguments to PowerShell?
It appears that powershell.exe does not fully evaluate script arguments when the
-Fileparameter is used. In particular, the$falseargument is being treated as a string value, in a similar way to the example below:Instead of using
-Fileyou could try-Command, which will evaluate the call as script:As David suggests, using a switch argument would also be more idiomatic, simplifying the call by removing the need to pass a boolean value explicitly: