built EXE don't handle command line switch parameters correctly

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 3 weeks 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
DrewEaston
Posts: 48
Last visit: Thu Mar 30, 2023 8:47 am

built EXE don't handle command line switch parameters correctly

Post by DrewEaston »

Product, version and build: PowerShell Studio 2017, 5.4.136
32 or 64 bit version of product: 64 bit
Operating system: Windows 7 Home Premium & Windows 7 Enterprise
32 or 64 bit OS: 64 bit
PowerShell Version: 4.0 & 5.0

If a ps1 file has command line parameters that are of the type [switch] and you compile them to EXE files the options can never be activated with the EXE.

Create a script with this code:

param
(
[Parameter(Mandatory = $false)]
[ValidateRange(0, 5)]
[Alias('d', 'dl')]
[int]
$debugLevel = 0,
[switch]
$information = $false,
[Alias('tst')]
[switch]
$test = $false
)
Write-Host "`n `$debugLevel: '$debugLevel'"
Write-Host "`n`$information: '$information'"
Write-Host "`n `$test: '$test'"
Write-Host "`n wait 3 seconds to exit..."
Start-Sleep -Seconds 3

and run it as a ps1 and you will see that you CAN set the [switch] parameters as desired anyway that PowerShell allows.

Now build an EXE from PowerShell Studio and you will find that you CANNOT ever get these to "switch".

My results were the same no matter what PowerShell version I was using or what was available on the machine.

Let me know if you can reproduce this or not.

Here are my "Script Packager" options:
Script-Engines_selections
Script-Engines_selections
2017.03.07_Deploy_ScriptPackager_Script-Engine_selections.png (65.1 KiB) Viewed 3533 times
Here are my "Execution Restrictions" options:
Execution-Restrictions_selections
Execution-Restrictions_selections
2017.03.07_Deploy_ScriptPackager_Execution-Restrictions_selections.png (45.78 KiB) Viewed 3533 times
Gruß,
Drew
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: built EXE don't handle command line switch parameters correctly

Post by DevinL »

This is the expected behavior since PowerShell can't convert the switch parameter into a String which is the only thing the engine will accept as a parameter.

For more information, please see this lovely blog series by JuneB: https://www.sapien.com/blog/2015/11/30/ ... able-file/

In particular, the section titled Change Parameter Values to Strings covers how to handle passing a switch parameter to an executable.
DevinL
SAPIEN Technologies, Inc.
User avatar
DrewEaston
Posts: 48
Last visit: Thu Mar 30, 2023 8:47 am

Re: built EXE don't handle command line switch parameters correctly

Post by DrewEaston »

OK Thanks for the Info.

I did some testing after looking at June's blog entry, yes that is an OK substitute for the switches. And it works. Good to go.

With some small modifications I can also allow 'T' for true and 'F' for false and of course I can set the default of "False"

Now I have even MORE questions:
1. If you guys have known this all along then WHY does the builder NOT warn of ALL of these pit falls?

2. Why does it NOT show the best solutions to us or at least a link to the most current stuff like the LINK you sent me?

3. Why does the EXE build NOT have the ability to automatically switch the switches to your own suggested solution and inform me of the REQUIRED changes?
... (Do you want EVERYONE to report such things?)

4. ALL command line parameters are strings and always were because the command line arguments are one big string from the OS to the application. Even in VBScript we had to, and still have to, have our own code for an argument to act like a switch.

Code in Point:
VBScript Sample code for a Switch Parameter
VBScript Sample code for a Switch Parameter
2017.03.13_11-07-02_Sample_VBS_code_for_A_CommandLine_switch.png (5.77 KiB) Viewed 3484 times
As you can see in the above code the programmer can, or has to, convert the 'string' to a Logical value. Aliases can also be handled, as many as the programmer wants.

5. In the SAPIEN PowerShell Debugger we/you also pass the command line options as one BIG string and it also has to be properly interpreted. Can't you guys do the same thing inside the EXE file?
... What exactly are you calling from inside this EXE?
... Does it just start the PowerShell.exe engine giving it the packaged scripts?
... Or is it your own PowerShell libraries?
... Why can't the parameters be passed just like the debugger gets them?

I hope you can identify with me on these Points.

Let me know if you guys change anything.

Gruß,
Drew
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: built EXE don't handle command line switch parameters correctly

Post by DevinL »

I'll speak to the team about your points and get back to you when I have some more information.
DevinL
SAPIEN Technologies, Inc.
User avatar
Alexander Riedel
Posts: 8488
Last visit: Tue Apr 16, 2024 8:42 am
Answers: 20
Been upvoted: 37 times

Re: built EXE don't handle command line switch parameters correctly

Post by Alexander Riedel »

1. If you guys have known this all along then WHY does the builder NOT warn of ALL of these pit falls?
It is quite hard to guess what people know or don't know, so we go by feedback. If a warning is warranted, we add it.

2. Why does it NOT show the best solutions to us or at least a link to the most current stuff like the LINK you sent me?
The output of a command line tool really is not a place to educate users about best practices or coding examples. That's kind of what blogs are for nowadays :D
That is why we write them and publish them.

3. Why does the EXE build NOT have the ability to automatically switch the switches to your own suggested solution and inform me of the REQUIRED changes?
... (Do you want EVERYONE to report such things?)
Automatically implies always without fail and without user intervention. We learned the hard way that some things are best left to the human brain for processing
rather than applying a "we know best attitude". We do not switch your switches because the tool just makes an exe out of your script, it doesn't analyze your code.
If we add an automatic switch switcher, you or someone will find a case where it switches the wrong switch. Then you ask for a switch to turn off the automatic
switch switcher. The next guy will ask for a default switch to turn off the automatic switch switcher and ask for a switch to just turn warnings into errors so he
can manually switch his switches whenever he wants to switch them. So then I ask you, who switches the switches?
(Sorry, I REALLY REALLY could not resist that one.)

4. ALL command line parameters are strings and always were because the command line arguments are one big string from the OS to the application. Even in VBScript we had to, and still have to, have our own code for an argument to act like a switch.
Yes, all command line arguments are strings, they always have been.

As you can see in the above code the programmer can, or has to, convert the 'string' to a Logical value. Aliases can also be handled, as many as the programmer wants.
Agreed. I think I am missing a point here.

5. In the SAPIEN PowerShell Debugger we/you also pass the command line options as one BIG string and it also has to be properly interpreted. Can't you guys do the same thing inside the EXE file?
No

... What exactly are you calling from inside this EXE?
In the engines titled "SAPIEN PowerShell ..." the PowerShell engine provided by Microsoft though its defined API.
Why is this relevant?

... Does it just start the PowerShell.exe engine giving it the packaged scripts?
Depending on the engine you pick, yes or no.
The description part of the engine selector displays if a temporary file is used or not. Obviously an actual file means the original tool is used (powershell.exe, CScript.exe, WScript.exe etc.), if it says "In memory" the engine is used by means of an API.

... Or is it your own PowerShell libraries?
We do not have 'powershell libraries'.

... Why can't the parameters be passed just like the debugger gets them?
Because an exe is called with command line arguments, the communication with the debugger is different for more reasons than I want to list here.
Alexander Riedel
SAPIEN Technologies, Inc.
This topic is 7 years and 3 weeks 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.