Powershell EXE not honoring parameters

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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 5 years and 11 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.
Locked
User avatar
mjphelan
Posts: 5
Last visit: Tue Mar 26, 2024 7:06 am

Powershell EXE not honoring parameters

Post by mjphelan »

Hi Folks,

This may seem a bit strange but there are reasons as to why I'm doing things the way I am which I can't go into here. Rest assured this is a needed process.

I have two applications, but PowerShell scripts generated using PowerShell studio 2018 and compiled into Executables using the BUILD function.

Script Engine is set to SAPIEN PowerShell V3 host (Command Line)

My first application generates and validates a set of credentials using $Credential = New-Object System.Management.Automation.PSCredential this application has been compiled into an executable which programmatically calls a second application passing that $Credential object as an argument using the command line & .\$ApplicationName -CredThing $Output

If my second application is a PS1 file (exported form or not) everything run's as expected the secondary application run's, and during this phase validates authentication and runs it's tasks using the $credential object (in this case called $CredThing to ensure it's unique.)

The parameters in my second application are quite simply (and for the form, yes they are outside $form_load={}

param ([Parameter(Mandatory = $true)][System.Management.Automation.PSCredential]$CredThing)

Now if I compile my second form into an executable instead of the parameter being used, I am prompted for a login as if I have just run get-creds, only the full command path run from application1 is in the username box.

Any thoughts?
User avatar
mjphelan
Posts: 5
Last visit: Tue Mar 26, 2024 7:06 am

Re: Powershell EXE not honoring parameters

Post by mjphelan »

I should add, the error I'm getting is

Line 1: Cannot bind argument to parameter 'CredThing' because it is null.

(but it's most definately not)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell EXE not honoring parameters

Post by jvierra »

You cannot pass objects on a command line. Command lines only accept text.

If you are calling a PS script then you can pass objects.

Call a script line this:

c:\temp\myscript.ps1 -ArgName $argument

You error says that you are trying to pass a null object.
User avatar
mjphelan
Posts: 5
Last visit: Tue Mar 26, 2024 7:06 am

Re: Powershell EXE not honoring parameters

Post by mjphelan »

Like I said in my origional post, when this is done with a PS1 file it works perfectly. So I'm afraid sir you are incorrect.

The exact same code as a PS1 works, as a compiled EXE does not.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell EXE not honoring parameters

Post by jvierra »

If you call an EXE with arguments they will be passed as text and not as objects.

If you pass a null object in all cases you will get that error.

You are creating "$credential" and passing "$output". Is that what you want to do.
User avatar
mjphelan
Posts: 5
Last visit: Tue Mar 26, 2024 7:06 am

Re: Powershell EXE not honoring parameters

Post by mjphelan »

In the origional source code, $output contains the credential object.

And once again, the EXE has been compiled using PowerShell Studio's "Build" function.
User avatar
mjphelan
Posts: 5
Last visit: Tue Mar 26, 2024 7:06 am

Re: Powershell EXE not honoring parameters

Post by mjphelan »

Sample code, just run "runme.ps1" you will be prompted for credentials, and if you are on a domain joined machine your credentials will be validated. The second app will then launch as a PS1, the credentials validated, then as an EXE where you will get prompted (which is not correct)
Attachments
sample_code.zip
(91.88 KiB) Downloaded 87 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell EXE not honoring parameters

Post by jvierra »

As I have noted more than once, you cannot pass objects to an EXE.

The following line will not work:

& .\source.exe -CredThing $Credentials

Also the "&" is unnecessary and should not be used here.

An EXE can only be passed strings. All objects will be converted to strings. You command effectively passes the results of this:

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

Re: Powershell EXE not honoring parameters

Post by davidc »

For details on how to handle parameters in an packaged executable, please refer to the following article:

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