Certain parameters do not run when compiled as an EXE even though they do as a .ps1

Use this forum to ask questions after your subscription maintenance expires or before you buy. Need information on licensing or pricing? Questions about a trial version? This is the right place for you. No scripting questions, please.
Forum rules
DO NOT POST SUBSCRIPTION NUMBERS, LICENSE 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.
This topic is 2 years and 6 months 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
jasonrutherford
Posts: 5
Last visit: Tue Feb 14, 2023 7:50 pm

Certain parameters do not run when compiled as an EXE even though they do as a .ps1

Post by jasonrutherford »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build: PowerShell Studio 2021 5.8.188
Operating system: Windows 10 20H2
PowerShell version(s): Windows PowerShell 5.1.19041.1151
32-bit version of software? No

*** Please add details and screenshots as needed below. ***
I have a script that I compiled to an EXE. When attempting to run the EXE, certain parameters do not work, even though they do as .PS1. The parameters that do not work are of type [string]. Yes, I have [switch] parameters, and I understand those are not fully supported, but the switches run fine, and all other [string] parameters are recognized. It is just specific parameters and there is no indication as to why this is occurring.


DO NOT POST LICENSES, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM
User avatar
brittneyr
Site Admin
Posts: 1649
Last visit: Mon Mar 18, 2024 1:47 pm
Answers: 38
Been upvoted: 30 times

Re: Certain parameters do not run when compiled as an EXE even though they do as a .ps1

Post by brittneyr »

Brittney
SAPIEN Technologies, Inc.
User avatar
Alexander Riedel
Posts: 8472
Last visit: Mon Mar 18, 2024 2:59 pm
Answers: 19
Been upvoted: 37 times

Re: Certain parameters do not run when compiled as an EXE even though they do as a .ps1

Post by Alexander Riedel »

It would help if you provide the exact parameters used, a sample value set you use and what exactly "not working" means.
That includes how you use the parameters in your code. Please understand that we cannot see your screen or your code.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
jasonrutherford
Posts: 5
Last visit: Tue Feb 14, 2023 7:50 pm

Re: Certain parameters do not run when compiled as an EXE even though they do as a .ps1

Post by jasonrutherford »

The "exact" parameters are relatively inconsequential, right? They are custom, so it wouldn't be relevant, unless I'm missing something. But, for your edification, here's an example:

param(
[string]$SuperLongFilePathName,
[string]$Hours,
[switch]$EnablePath,
[switch]$OpenFile
)

By "not working", the EXE just fails to run when using "-Hours 12" or the likes. The event viewer shows an exception code for the application of "Exception code: 0xc0000409".
User avatar
Alexander Riedel
Posts: 8472
Last visit: Mon Mar 18, 2024 2:59 pm
Answers: 19
Been upvoted: 37 times

Re: Certain parameters do not run when compiled as an EXE even though they do as a .ps1

Post by Alexander Riedel »

No, they are not inconsequential. Just from your parameters I see $SuperLongFilePathName
It is quite important what you specify there and how long that is. Windows has a limit on how long a complete command line can be.
A 0xc0000409 is a stack overflow exception. Without context, that means nothing, as we cannot tell if it is the exe engine parsing your parameters or your code doing something recursive.
So all information is of consequence. So an exact value set that you use to produce this is essential.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
jasonrutherford
Posts: 5
Last visit: Tue Feb 14, 2023 7:50 pm

Re: Certain parameters do not run when compiled as an EXE even though they do as a .ps1

Post by jasonrutherford »

Fair enough. Here are a list of params, and I'll follow with the command line used to produce the error:

param(
[string]$BackgroundColor = "8dc63f",
[string]$UpdateAuthority = "WUfB",
[string]$LogSizeKB = "500",
[string]$RebootCountdownHours = "8",
[string]$RebootCountdownMinutes = "0",
[string]$RebootCountdownSeconds = "0",
[string]$MaxDeferrals = "3",
[string]$DeferOption0 = "1",
[string]$DeferOption1 = "4",
[string]$DeferOption2 = "8",
[string]$PendingActionsCheckIntervalMinutes = "15",
[string]$WindowsUpdateRemediationLevel = 0,
[switch]$DisableBalloonNotifications,
[switch]$EnableCloseButton,
[switch]$ShowInTaskbar,
[switch]$EnableDemo,
[switch]$DisableUpdateScan,
[switch]$DisableRebootScan,
[switch]$DisableUpgradeScan,
[switch]$OverrideWUNotifications,
[switch]$OverrideCMUpdateNotifications,
[switch]$EnableVerboseLogging,
[switch]$AlwaysTopmost,
[switch]$RetryFailedInstallation
)

Command line:
install.ps1 -AlwaysTopmost -OverrideWUNotifications -OverrideCMUpdateNotifications -DisableUpdateScan -DisableRebootScan -MaxDeferrals 2 -BackgroundColor 00294D -UpdateAuthority HYBRID -RebootCountdownHours 12

If I take off "-RebootCountdownHours 12" it works. So, I'm guessing (based on what you stated above) my command line is too long... although, that doesn't explain to me why I can run it as a .ps1. Is that limited to EXE?
User avatar
Alexander Riedel
Posts: 8472
Last visit: Mon Mar 18, 2024 2:59 pm
Answers: 19
Been upvoted: 37 times

Re: Certain parameters do not run when compiled as an EXE even though they do as a .ps1

Post by Alexander Riedel »

The Windows command line limit is 32767. The maximum length of a command line in cmd.exe is 8191. You did not state how you packaged this particular exe, so I cannot tell which one applies.
If you look at how command line tools for almost any OS are usually designed, you will notice that parameter names are usually just long enough to be unique but as short as possible.
That is to leave room for the actual data.
So as an example you would use
install.ps1 -t -wu -cm -dus -drs -m 2 -bc 00294D -ua HYBRID -rc 12
Parameters are typically kept lowercase to make it easier on the user. You do not have to remember the exact case, the mnemonic will do and you do not need to use shift at all times while typing.
Your tool should also follow the practice of displaying an about message listing the possible parameters with an explanation when called without parameters.
If the absence of any parameter is defined as a specific action, something like -help or -? should display information.
2021-08-26_9-18-14.png
2021-08-26_9-18-14.png (231.08 KiB) Viewed 18192 times
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
Alexander Riedel
Posts: 8472
Last visit: Mon Mar 18, 2024 2:59 pm
Answers: 19
Been upvoted: 37 times

Re: Certain parameters do not run when compiled as an EXE even though they do as a .ps1

Post by Alexander Riedel »

Just to answer your other question why this works as a .PS in a PowerShell console. The PowerShell team decided how parameters are passed within their own environment and what limits apply and when.
These conditions however do not apply to anything outside of the purview of PowerShell. So as soon as you call Windows or console executable files, the general OS rules apply again.
Alexander Riedel
SAPIEN Technologies, Inc.
This topic is 2 years and 6 months 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.