How to get the error code (ErrorLevel) from PowerShell into Windows command prompt? How to get the error code (ErrorLevel) from PowerShell into Windows command prompt? windows windows

How to get the error code (ErrorLevel) from PowerShell into Windows command prompt?


Try this code in cmd.exe shell :

C:> powershell -noprofile -command " try { 1/0 } catch {exit 22}"C:> echo %errorlevel%22

So, add your commands in try block. If it happens that you call non powershell command you could exit $LastExitCode (but from within try block).


EDIT

OK, I know what is going on with your non-working code, it never passed trough catch block as Get-Content produces non-terminating error. You have to take care about that:

C:> Powershell -Command "try { (Get-Content HvsvmAppserver.ref -ea stop) -replace '-Dfr.base0.role=', '-Dfr.base0.role=USR_ROLE'| Set-Content -encoding ASCII HvsJvmAppserver.ref -ea stop } catch {$_; exit 123 }"Get-Content : Cannot find path 'C:\Users\majkinetor\HvsvmAppserver.ref' because it does not exist.At line:1 char:8+ try { (Get-Content HvsvmAppserver.ref -ea stop) -replace '-Dfr.base0. ...+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : ObjectNotFound: (C:\Users\majkinetor\HvsvmAppserver.ref:String) [Get-Content], ItemNotFoundException    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommandC:> echo %errorlevel%123

Notice the -ea stop that I added to both commands (shorter then ErrorAction). My solution was correct the first time, however, you need to understand that script must be designed correctly too. You might also want to use trap instead catch, but you still have to use EA stop. Perhaps the shortest general solution that doesn't depend too much on a script is:

   $ErrorActionPreferance = 'Stop'   <your script goes here, no need for -ea stop in it>   trap { exit 123 }

This will not work if the script in question changes $ErrorActionPreference (which rarely happens) but will work in all other cases.


So to put this as an answer based on my comment:

You are getting zero because that is the value you are passing. If you want to use either one of your variables as the exit code you have to set them to something.

To which you responded:

@ShawnMelton Thanks, but I haven't get it well....I don't want to make my own error code or personnalized error management ! All I want is that when an error occured in my powershell (code, scripts etc...), the error level of Dos get this error code (I don't want to make my own code). Can you illustrate by a code what do you mean ? I may have not understand your comment ? thanks for trying to help it's nice from your side.

Being that you are calling powershell.exe it is going to return an exit code back to the command prompt, always as zero (0). It will always be zero even if you execute PowerShell code that is incorrect. The powershell.exe itself is successfully executing any command you pass to it so unless the executable itself errors for some reason, you always get zero. If you want PowerShell.exe to return something other than zero you are required to use the exit xx in your PowerShell command, where xx is an integer.enter image description here

Which the point made of not trying to call PowerShell from a command prompt still stands, whether you want to here it or not. You are not guaranteed that even using something like exit 99 is going to always overwrite the executable returning zero...it is just the way it works. I got various results when I tried it on my laptop with Windows 10. I can say that I have used technique before in production when I have had to call PowerShell scripts from SSIS packages, and it served the purpose. However in that situation I was not calling it from a batch file so it worked as expected.


The suggestion by @majkinetor definitely works. I mean this one:

powershell -Command "try {(gc "C:\test.txt" -ea stop) -creplace 'test', 'work' | Out-File -encoding ASCII "C:\test.txt" -ea stop} catch{ exit 1}"

If the file path is correct, the string is replaced. If the file path is incorrect - the error level in the cmd session is set to 1 with the above snippet.

The issue I encountered, is in the case that I want this to fail a Jenkins build. I use build step Execute Windows batch command and the trick is that you need to have only the above command if you want it to fail the build upon error. If you have another one afterwards and it is successful - the build is successful.