IT漫步

技术生活札记©Yaohui

Powershell does not support pass Boolean parameter when -File parameter is used

Let’s say there is a Powershell script file named test.ps1 with the following content:

param ([Boolean] $FromSchTask)

if ($FromSchTask)
{
    echo Yes
} else {
    echo No
}

When I run this script with -File parameter in command prompt by powershell.exe, for example:

C:\>powershell.exe -ExecutionPolicy bypass -File ./test.ps1 -FromSchTask $True

It will throw the following error message:

C:\test.ps1 : Cannot process argument transformation on parameter 'FromSchTask'. Cannot convert value "System.String" to type
"System.Boolean". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0.
    + CategoryInfo          : InvalidData: (:) [test.ps1], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,test.ps1

As mentioned in this comments in stackoverflow: https://stackoverflow.com/a/7302626/9657874, it seems the -File parameter does not support pass Boolean parameter to script file. After change -File to -Command, it works like a clarm:

C:\>powershell.exe -ExecutionPolicy bypass -Command ./test.ps1 -FromSchTask $True
Yes

Leave a Reply

Your email address will not be published.

Proudly powered by WordPress