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.
This topic is 4 years and 1 month 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.
Locked
User avatar
Nillth
Posts: 34
Last visit: Mon Oct 30, 2023 10:52 pm
Has voted: 2 times

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 20
Been upvoted: 37 times

Re: Passing Switch parameters to packaged executable

Post by Alexander Riedel »

[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
Nillth
Posts: 34
Last visit: Mon Oct 30, 2023 10:52 pm
Has voted: 2 times

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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.
This topic is 4 years and 1 month 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.
Locked