Powershell V2 - Powershell can't convert $false into [switch] when using -File param - by johndog

Status : 

 


7
0
Sign in
to vote
ID 742084 Comments
Status Active Workarounds
Type Bug Repros 4
Opened 5/14/2012 4:40:48 PM
Access Restriction Public

Description

Running:

    powershell -File .\temp.ps1 -isit:$false

On a script that takes a switch (-isit) results in an error "Cannot process argument transformation on parameter 'isit'. Cannot convert value "System.String" to type "System.Management.Automation.SwitchParameter"

Sign in to post a comment.
Posted by David I McIntosh on 2/4/2015 at 11:55 AM
This is a duplicate of my comment from yesterday, but the formatting yesterday was aweful and I cannot see how to edit comments, so I am re-posting with proper formatting. Any mods are welcome to delete yesterdays comment and edit out this disclaimer.
It gets even worse.
Notice the very clear text of the error message: " parameters of this type only accept booleans or numbers, use $true, $false, 1 or 0 instead".
So, we know    powershell -File .\temp.ps1 -isit:$false
does not work because the $false object is replaced by the string False. Similarly
    powershell .\temp.ps1 -isit:$falsefails. What if we try `$false, as in:
    powershell -File .\temp.ps1 -isit:`$false
this fails. What if we try without the -File :
    powershell .\temp.ps1 -isit:`$false
Success! Very very ugly, and not really workable as a generic workaround, but maybe we're on to something. So, powershell likes $true and $false, as long as you eacape the $ so that it is processed by the target powershell instance, not the invoking instance. OK, but the error message says VERY CLEARLY "$true, $false, 1 or 0", so lets try 0 or 1:
    powershell .\temp.ps1 -isit:1FAIL!!!!!! :<p>
powershell : ...: Cannot process argument transformation on parameter 'isit'. Cannot convert the "1" value of type "System.Int32" to type "System.Management.Automation.SwitchParameter".
At line:1 char:1+ powershell .\temp.ps1 -isit:1+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo         : NotSpecified: (C:\Users\...itchParameter".:String) [], RemoteEx
ception    + FullyQualifiedErrorId : NativeCommandError At line:1 char:18
+ .\temp.ps1 -isit:1+                 ~
    + CategoryInfo         : InvalidData: (:) [temp.ps1],
ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,temp.ps1
</p>
So it even knows that "1" is an integer, but it cannot, as advertised, convert an integer 0 or 1 into a boolean! How bad is that?
Posted by David I McIntosh on 2/3/2015 at 1:59 PM
It gets even worse.
Notice the very clear text of the error message: " parameters of this type only accept booleans or numbers, use $true, $false, 1 or 0 instead".
So, we know    powershell -File .\temp.ps1 -isit:$false
does not work because the $false object is replaced by the string False. Similarly
    powershell .\temp.ps1 -isit:$falsefails. What if we try `$false, as in:
    powershell -File .\temp.ps1 -isit:`$false
this fails. What if we try without the -File :
    powershell .\temp.ps1 -isit:`$false
Success! Very very ugly, and not really workable as a generic workaround, but maybe we're on to something. So, powershell likes $true and $false, as long as you eacape the $ so that it is processed by the target powershell instance, not the invoking instance. OK, but the error message says VERY CLEARLY "$true, $false, 1 or 0", so lets try 0 or 1:
    powershell .\temp.ps1 -isit:1FAIL!!!!!! :
powershell : ...: Cannot process argument transformation on parameter 'isit'. Cannot convert the "1" value of type "System.Int32" to type "System.Management.Automation.SwitchParameter".
At line:1 char:1+ powershell .\temp.ps1 -isit:1+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo         : NotSpecified: (C:\Users\...itchParameter".:String) [], RemoteEx
ception    + FullyQualifiedErrorId : NativeCommandError At line:1 char:18
+ .\temp.ps1 -isit:1+                 ~
    + CategoryInfo         : InvalidData: (:) [temp.ps1],
ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,temp.ps1
So it even knows that "1" is an integer, but it cannot, as advertised, convert an integer 0 or 1 into a boolean! How bad is that?
Posted by Jakub Berezanski on 7/9/2014 at 3:11 PM
Actually, the proposed workaround with curly braces does not seem to work, as is evident with a slight modification to the script:

temp.ps1:

param([switch]$isit, [string]$str)
"isit: $isit"
"Bound parameters:"
$PSBoundParameters

>powershell -version 2 -File .\temp.ps1 {-isit:$False}
isit: False
Bound parameters:

Key                                                                                 Value
---                                                                                 -----
str                                                                                 {-isit:$False}

>powershell -version 2 -File .\temp.ps1 {-isit:$True}
isit: False
Bound parameters:

Key                                                                                 Value
---                                                                                 -----
str                                                                                 {-isit:$True}

>powershell -version 2 -File .\temp.ps1 {-isit:$True} xyz
isit: False
Bound parameters:

Key                                                                                 Value
---                                                                                 -----
str                                                                                 {-isit:$True}


for comparison:

>powershell -version 2 -Command .\temp.ps1 -isit:$True xyz
isit: True
Bound parameters:

Key                                                                                 Value
---                                                                                 -----
isit                                                                                 True
str                                                                                 xyz
Posted by johndog on 5/17/2012 at 11:17 AM
Here is help:

>powershell -version 2 -help

PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>]
    [-NoLogo] [-NoExit] [-Sta] [-NoProfile] [-NonInteractive]
    [-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]
    [-WindowStyle <style>] [-EncodedCommand <Base64EncodedCommand>]
    [-File <filePath> <args>] [-ExecutionPolicy <ExecutionPolicy>]
    [-Command { - | <script-block> [-args <arg-array>]
                 | <string> [<CommandParameters>] } ]

...


-File
    Execute a script file.

...


So, no, this format is not at all described in the help. There is text for -Command about using {} around a script block, but this case is not -Command, and it's not a script block.
Posted by Microsoft on 5/16/2012 at 3:59 PM
Please use this format (this is described in powershell.exe -help)
powershell.exe -version 2 -file .\temp.ps1 {-isit:$false}