Home Dashboard Directory Help
Search

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


Status: 

Active


4
0
Sign in
to vote
Type: Bug
ID: 742084
Opened: 5/14/2012 4:40:48 PM
Access Restriction: Public
1
Workaround(s)
view
2
User(s) can reproduce this bug

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"

Details
Sign in to post a comment.
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}
Sign in to post a workaround.
Posted by johndog on 5/14/2012 at 4:42 PM
If you run the script with this command-line format, it works:

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

Or like this:

     powershell .\temp.ps1 -isit:$false

However, neither of these formats are documented in help.