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