KySoto
KySoto

Reputation: 428

error saying it cant convert string to bool

I am trying to write a function that echo's the input if my script is running in debug mode.

[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" 

the error i get is

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

This error doesnt make sense to me since i am passing a boolean into the boolean and a string into the psobject.

Upvotes: 0

Views: 185

Answers (1)

Maximilian Burszley
Maximilian Burszley

Reputation: 19684

You have two options

  1. Correct your syntax error: You must pass parameters in the form of -param [value]:

    -isDebug $debugmode
    
  2. Use the right tool for the job, a [switch] parameter:

    function DebugWrite-Output([switch] $isDebug, $inputObject) {
        if ($isDebug.IsPresent) { ...
    

    Then you call it by just including the switch:

    -isDebug
    

Upvotes: 2

Related Questions