从非powershell终端传递布尔powershell参数



有时我会从Linux bash/shell脚本中调用powershell脚本,比如:

pwsh MyScript.ps1 win-x64 false

在我的MyScript.ps1文件中,我设置了如下参数:

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

我得到了第二个参数的错误:

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.

我尝试传递'$false'0,但它将所有内容都视为字符串。当从PWSH终端外部调用powershell脚本时,我如何让它将我的字符串布尔值强制转换为实际的powershell布尔类型?

我建议使用[开关]

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

它适用于我:

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

事实上,您的代码正在使用:

pwsh ".MyScript.ps1" toto -singleFile 1

最新更新