Function 表示无法将字符串转换为布尔值时出错

Function 表示无法将字符串转换为布尔值时出错,function,powershell,parameters,Function,Powershell,Parameters,我正在尝试编写一个函数,如果我的脚本在调试模式下运行,该函数将作为输入 [bool]$debugmode = $true #one liner version for manually pasting into the powershell console #Function DebugWrite-Output([bool]$isDebug,$inputObject){if ($isDebug -eq $true){Write-Output $inputObject}} Function Deb

我正在尝试编写一个函数,如果我的脚本在调试模式下运行,该函数将作为输入

[bool]$debugmode = $true
#one liner version for manually pasting into the powershell console
#Function DebugWrite-Output([bool]$isDebug,$inputObject){if ($isDebug -eq $true){Write-Output $inputObject}}
Function DebugWrite-Output([bool]$isDebug,$inputObject)
    {if ($isDebug -eq $true){
        Write-Output $inputObject
    }
}
DebugWrite-Output -isDebug = $debugmode -inputObject "Loading create access file function" 
我得到的错误是

DebugWrite-Output : Cannot process argument transformation on parameter 'isDebug'. Cannot convert value "System.String" to type "System.Boolean". Boolean parameters accept only Boolean values and numbers, such as 
$True, $False, 1 or 0.
At C:\Users\*****\source\repos\Powershell Scripts\Modular-Export.ps1:9 char:28
+ DebugWrite-Output -isDebug = $debugmode -inputObject "Loading create  ...
+                            ~
    + CategoryInfo          : InvalidData: (:) [DebugWrite-Output], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,DebugWrite-Output
这个错误对我来说没有意义,因为我正在将布尔值传递给布尔值,将字符串传递给psobject。

您有两个选项

  • 更正语法错误:必须以
    -param[value]
    的形式传递参数:

    -isDebug $debugmode
    
  • 使用正确的作业工具,一个
    [开关]
    参数:

    function DebugWrite-Output([switch] $isDebug, $inputObject) {
        if ($isDebug.IsPresent) { ...
    
    然后,只需包含开关即可调用它:

    -isDebug
    
  • 你有两个选择

  • 更正语法错误:必须以
    -param[value]
    的形式传递参数:

    -isDebug $debugmode
    
  • 使用正确的作业工具,一个
    [开关]
    参数:

    function DebugWrite-Output([switch] $isDebug, $inputObject) {
        if ($isDebug.IsPresent) { ...
    
    然后,只需包含开关即可调用它:

    -isDebug
    

  • 您想要的是
    [switch]
    ,而不是布尔标志。。。我们将作为答案发布,这样我就可以在@TheIncorrigible1this>>
    -isDebug=$debugmode
    您想要的
    [switch]
    ,而不是一个布尔标志。。。我们把答案贴出来,这样我就可以在不可纠正的地方接受它了。这个>>>
    -isDebug=$debugmode