Unable to get correct $lastexitcode from compiled EXE

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.
mdyunusraza
Posts: 1
Last visit: Mon Jul 05, 2021 5:18 am

Unable to get correct $lastexitcode from compiled EXE

Post by mdyunusraza »

Hi All,

I have created a simple GUI app with buttons and each button click will give an exit code. The script is working fine and I am getting the exit code when i run the script. However I am not able to retrieve the exit code in powershell using $LASTEXITCODE variable. Its always empty or sometimes 0.

For e.g. the compiled EXE is called TEST.EXE and if I call it inside my powershell script as mentioned below, then the $lastexitcode is always empty or 0. But i know the exit code is coming as i can see it

Code: Select all

 
 Start-Process test.exe
 $lastexitcode

Code: Select all

PS C:\temp> .\test.exe
22
PS C:\temp> $lastexitcode                                                                                              
 0
PS C:\temp> .\test.exe                                                                                                  
11
PS C:\temp> $lastexitcode                                                                                               
0
                                                                                              0


I have the following in the "Startup.pss" file. I commented the default option as it was giving only 0

Code: Select all

#$script:ExitCode = 0 #Set the exit code for the Packager
$script:lastexitcode
Then under the form script (Mainform.psf) for the two buttons I have the following

Code: Select all

$buttonYes_Click = {
	$script:lastexitcode = 22
	return $script:lastexitcode
	}
	
	$buttonNo_Click={
	$script:lastexitcode = 11
	return $script:lastexitcode
	
    }

User avatar
brittneyr
Site Admin
Posts: 821
Last visit: Thu Aug 05, 2021 11:22 am
Answers: 16
Been upvoted: 13 times

Re: Unable to get correct $lastexitcode from compiled EXE

Post by brittneyr »

To set the exit code when packaging a script to an executable, $script:ExitCode needs to be set.
Brittney Ryn
SAPIEN Technologies, Inc.