void.pointer
void.pointer

Reputation: 26335

Passing Boolean powershell parameters from a non-powershell terminal

Sometimes I invoke powershell scripts from Linux bash/shell scripts like so:

pwsh MyScript.ps1 win-x64 false

And in my MyScript.ps1 file, I set up parameters like so:

[CmdletBinding()]
param (
    [Parameter(Mandatory = $true)]
    [string] $runtime,
    [Parameter()]
    [bool] $singleFile = $true
)

I get an error for the second parameter:

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

I tried passing '$false' as well as 0 but it treats everything as a string. When invoking powershell scripts from outside of a PWSH terminal, how do I get it to coerce my string-boolean value into an actual Powershell boolean type?

Upvotes: 2

Views: 506

Answers (1)

JPBlanc
JPBlanc

Reputation: 72610

I propose to use [switch]

[CmdletBinding()]
param (
    [Parameter(Mandatory = $true)]
    [string] $runtime,
    [Parameter()]
    [switch] $singleFile
)
Write-Host $runtime

It works for me with :

pwsh ".\MyScript.ps1" "toto" -singlefile

In fact your code is working with :

pwsh ".\MyScript.ps1" toto -singleFile 1

Upvotes: 4

Related Questions