Reputation: 428
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
Reputation: 19684
You have two options
Correct your syntax error: You must pass parameters in the form of -param [value]
:
-isDebug $debugmode
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