Page 1 of 1

Passing Switch parameters to packaged executable

Posted: Thu Feb 06, 2020 10:15 pm
by Nillth
I was looking for a way to pass switch parameters to a packaged exe and came across the following article.
https://www.sapien.com/blog/2015/11/30/ ... able-file/

While this is a clean workaround for not being able to handle [switch]'s, it still requires a value to be presented..
as such, in my code i have been using the following.

Code: Select all

param
(
	[ValidateSet('True', 'False',"", IgnoreCase = $true)]
	[string]$SwitchValue = "False"
)

if ($SwitchValue -ne "false") { [switch]$SwitchValue= $true }
else { [switch]$SwitchValue= $false }
which resolves the issue altogether in that if the -SwitchValue is present on the command line, the switch becomes $True.
however....
If I use the Parameter editor it removes the empty value from the ValidateSet.

Code: Select all

param
(
	[ValidateSet('True', 'False', IgnoreCase = $true)]
	[string]$SwitchValue = "False"
)

if ($SwitchValue -ne "false") { [switch]$SwitchValue= $true }
else { [switch]$SwitchValue= $false }
It would be really nice, if it could be modified to leave the empty value "" in as it is a valid value.

Packaged.exe -SwitchValue

Re: Passing Switch parameters to packaged executable

Posted: Thu Feb 06, 2020 10:34 pm
by jvierra
This is how I do it.

Code: Select all

param(
    [switch]$test
)
if($PSBoundParameters.ContainsKey('test')){
    $test = $true
}
Write-Host $test
Run as normal:
TestsSwitch.exe -test
TestSwitch,exe

This will return verification that the switch has been detected. It save all of the validation code Just use the switch as normal.

Re: Passing Switch parameters to packaged executable

Posted: Fri Feb 07, 2020 12:12 am
by Alexander Riedel
[Topic moved by moderator]

Re: Passing Switch parameters to packaged executable

Posted: Sun Feb 16, 2020 4:05 pm
by Nillth
Interesting,
The packager did not previously support [switch] options as per the section "Change Parameter Values to Strings" in the URL https://www.sapien.com/blog/2015/11/30/ ... able-file/

Re: Passing Switch parameters to packaged executable

Posted: Sun Feb 16, 2020 4:13 pm
by jvierra
It still doesn't. I just showed you a trivk that can detect a parameter by name. It is useful for detecting a switch and allowing you to set the value. It is not a robust solution but it can be useful.