Passing Switch parameters to packaged executable

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
Nillth
Posts: 31
Joined: Thu Aug 01, 2013 6:14 pm
Location: Melbourne, Australia

Passing Switch parameters to packaged executable

Post 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

jvierra
Posts: 14452
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Passing Switch parameters to packaged executable

Post 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.

User avatar
Alexander Riedel
Posts: 7377
Joined: Tue May 29, 2007 4:43 pm

Re: Passing Switch parameters to packaged executable

Post by Alexander Riedel »

[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
Nillth
Posts: 31
Joined: Thu Aug 01, 2013 6:14 pm
Location: Melbourne, Australia

Re: Passing Switch parameters to packaged executable

Post 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/

jvierra
Posts: 14452
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Passing Switch parameters to packaged executable

Post 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.

Locked