Unable to pass boolean value to compiled ps1

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
DennisVH
Posts: 12
Last visit: Wed Nov 16, 2022 6:24 am

Unable to pass boolean value to compiled ps1

Post by DennisVH »

Product, version and build: PowerShell Studio 2016, 5.3.130
32 or 64 bit version of product: 64 bit
Operating system: Windows 7
32 or 64 bit OS: 64 bit
PowerShell Version: 3.0

When trying to pass a boolean value to a switch parameter (running a ps1 compiled to exe in a command prompt) the execution fails.
f.e. Test_1.0.exe -testswitch:$false
Line 1: A parameter cannot be found that matches parameter name 'testswitch:$false'.
Separating the $false value from the parameter also doesn't work.
f.e. Test_1.0.exe -testswitch $false
Line 1: Cannot process argument transformation on parameter 'testswitch'. Cannot convert value "System.String" to type 'System.Management.Automation.SwitchParameter". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0.
A possible reason is that powershell.exe doesn't fully evaluate the script arguments when -file parameter is used.

I'm able to reproduce the issue when running powershell.exe with -file parameter.
f.e. powershell -file .\Test_1.0.ps1 -testswitch:$false
Test_1.0.ps1 : cannot process argument transformation on parameter 'testswitch'. Cannot convert value "System.String" to type 'System.Management.Automation.SwitchParameter". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0.
+ CategoryInfo : InvalidData: (:) [Test_1.0.ps1], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Test_1.0.ps1
The solution is to run powershell.exe without the -File parameter:
f.e. powershell.exe .\Test_1.0.ps1 -testswitch:$false

When you compile the ps1 file to an exe, are you passing the -File parameter to powershell.exe? And if this is the case, is it possible to remove the -File parameter?
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Unable to pass boolean value to compiled ps1

Post by davidc »

Short answer: You have to change the switch parameter to a string parameter.

For the longer answer, please refer to the following article:

https://www.sapien.com/blog/2015/11/30/ ... able-file/
David
SAPIEN Technologies, Inc.
This topic is 7 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.