Passing Switch parameters to packaged executable

Ask your Windows 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.
Post Reply
User avatar
Nillth
Posts: 22
Joined: Thu Aug 01, 2013 6:14 pm
Location: Melbourne, Australia

Passing Switch parameters to packaged executable

Post by Nillth » Thu Feb 06, 2020 10:15 pm

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: 14155
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Passing Switch parameters to packaged executable

Post by jvierra » Thu Feb 06, 2020 10:34 pm

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: 7214
Joined: Tue May 29, 2007 4:43 pm

Re: Passing Switch parameters to packaged executable

Post by Alexander Riedel » Fri Feb 07, 2020 12:12 am

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

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

Re: Passing Switch parameters to packaged executable

Post by Nillth » Sun Feb 16, 2020 4:05 pm

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: 14155
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Passing Switch parameters to packaged executable

Post by jvierra » Sun Feb 16, 2020 4:13 pm

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.

Post Reply