Passing the actual return code from Powershell

三世轮回 提交于 2020-12-13 04:09:32

问题


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

回答1:


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.




回答2:


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:



来源:https://stackoverflow.com/questions/55798043/passing-the-actual-return-code-from-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!