Executable with Switches

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 5 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
rocky.pabillore
Posts: 13
Last visit: Wed Apr 28, 2021 10:24 am

Executable with Switches

Post by rocky.pabillore »

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: PoSh Studio 2018, 5.5.154.0
32 or 64 bit version of product: 64bit
Operating system: win 10 1803
32 or 64 bit OS: 64bit

*** Please add details and screenshots as needed below. ***
So, my question is that I'm trying to create an executable with switches, and with just the \test.ps1 -hello it out puts what I need, but when it is packaged, it does not output anything.




DO NOT POST SUBSCRIPTIONS, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Executable with Switches

Post by mxtrinidad »

I'm assuming you got the executable create "Test.exe" and that you got an argument (parameter) created to pass the value.
Again, we don't have much information from you on how the script look like.

So, I can guess the mistake is using the dash '-'. The executable is not a DOS-type command but a .NET PowerShell executable.
Remove the dash and just type the value:

Below is a code I created as an executable:

Code: Select all

<#	
	.NOTES
	===========================================================================
	 Created with: 	SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.155
	 Created on:   	8/27/2018 1:49 PM
	 Created by:   	Maximo Trinidad
	 Organization: 	SAPIEN Technologies, Inc.
	 Filename:    Testoutput.ps1 	
	===========================================================================
	.DESCRIPTION
		Sample to create an executable to passing a value.
#>

param ([string] $arg1)
	Write-Host "$arg1"

Below is the results:

Code: Select all

PS [21] > pwd

Path
----
C:\Users\max_t\Documents\SAPIEN\PowerShell Studio\Files\Testexecarg1\bin\x64


PS [28] > .\Testoutput.exe Hello
Hello
PS [30] > .\Testoutput.exe -Hello
## -> Displays nothing!
PS [32] >
I hope this help!
User avatar
rocky.pabillore
Posts: 13
Last visit: Wed Apr 28, 2021 10:24 am

Re: Executable with Switches

Post by rocky.pabillore »

Param (
[switch]$hello,
[switch]$no

)


if ($hello)
{
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Answer is: is present", 0, "Done", 0x1)
}

elseif ($no)
{
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Answer is: hello not present", 0, "Done", 0x1)
}
else
{

$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Answer is: hello not called", 0, "Done", 0x1)

} I then compile that as a V5 Host (Command Line).. Sorry i forgot to add the source code.
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Executable with Switches

Post by mxtrinidad »

To be clear, switches are not automatically build in PowerShell executable. You can only pass argument(s) to the existing parameter(s) without the dash.

So, it's possible only if you add more code, which add complexity, in order to expose switches. Even when using Visual Studio then with languages like C# or VB more code still is required.

Now, to keep in simple, just add dash to the argument value, and have the PowerShell code logic handle it.
I think this should be the simplest way.

In the case you may want to display a help for the command, more code will still be needed.

:)
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Executable with Switches

Post by davidc »

Please refer to the following article on how to handle parameters in an package executable:

https://info.sapien.com/index.php/packa ... table-file
David
SAPIEN Technologies, Inc.
This topic is 5 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.