2

I have a command-line exe utility that requires an inline password to call. Obviously, that's not ideal, so I've created a PS wrapper to encrypt the password and store it/call it from a PSCredential object.

All of that is working fine, but I need error handling. If I add exit $LASTEXITCODE to the end of the script as I've seen suggested, I just get a simple 0 or 1. This utility uses return codes for scenarios that need to be handled differently, not all of which are actually errors. How can I return the actual numeric code?

Edit: Thanks for the responses. So the issue is that the calling process is not getting the return code.

The PS script is basically just &"C:\blah.exe" followed by exit $LASTEXITCODE. It's not in a function.

When I run it from PS, I get the correct code, but when I call it externally, I don't, even though I still get the correct status (0 or 1).

PS C:\Powershell> .\RunTask.ps1 454713613
ErrorCode: 2
NominalStart: 2019-04-22 12:20:53.87
TimeEnded: 2019-04-22 12:20:56

PS C:\MoveIT_API\Powershell> $LASTEXITCODE
2
PS C:\MoveIT_API\Powershell> exit

C:\Powershell>powershell .\RunTask.ps1 454713613
ErrorCode: 2
NominalStart: 2019-04-22 12:22:51.98
TimeEnded: 2019-04-22 12:22:54

C:\Powershell>echo %ERRORLEVEL%
1
2
  • Could you give some more information? Code samples, example error codes, etc... Apr 22, 2019 at 17:04
  • Sorry for being vague. Post is updated. Apr 22, 2019 at 17:45

2 Answers 2

2

It's a two step process. First inside your PowerShell wrapper script you need to pass back the $LASTEXITCODE back out by Exiting with the $LASTEXITCODE from the .exe utility e.g.

$exe = "myExe.exe"
$ArgumentList = "/arst"

& $exe $ArgumentList

$Result = $LASTEXITCODE

#....

Exit $Result

You have to do this in your wrapper script because $LASTEXITCODE from the PowerShell prompt would always be 0 because yes the -wrapper- script ran successfully (it doesn't matter what the .exe exited with). By explicitly Exiting with a code you can control the exit code from the wrapper script, and yes, from the PowerShell prompt, you can get the right return code.

The second part of the equation is how you call PowerShell, because a simple call to PowerShell.exe will always return 0 because -yes- the process PowerShell.exe ran successfully, notwithstanding how your script actually exited. So the workaround I do is to use the -Command argument to simulate running the wrapper script from a prompt, like this:

powershell.exe -Command ". C:\MyScript.ps1; exit $LASTEXITCODE"

This tells PowerShell.exe to execute the command MyScript.ps1, and explicitly exit PowerShell with the Exit code from my script. This then sets the %ERRORLEVEL% variable with the exit code from the script, and not PowerShell.exe.

0
1

Double check you're outputting the error code correctly. Lemme explain...

$LASTEXITCODE contains the exit code of the last Win32 executable execution; akin to %ERRORLEVEL%.

I've got a commandline utility named "sample.exe" that takes a integer value as a parameter and returns it as an exit code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sample
{
    class Program
    {
        static void Main(string[] args)
        {

            System.Console.WriteLine("Running...");

            int returnCode = 0;

            try
            {
                if (args.Length > 0)
                {
                    returnCode = int.Parse(args[0]);
                }
            } finally
            {
                Environment.Exit(returnCode);
            }
        }
    }
}

When I call it then $LASTEXITCODE, this is what I get:

call_to_last_exit_code

1
  • 2
    you could just use ( cmd /c "exit 22" ) to do that Apr 22, 2019 at 17:24

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.