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

5

12

I am trying to launch a PowerShell command through Windows command prompt to replace a text into a file. The problem is that I am always getting ErrorLevel = 0 even when my PowerShell command is not working. My question is how to get an error code into ErrorLevel when I launch PowerShell through cmd? Please note that I would like to make this through the Windows command prompt and not by creating a script a part.

Here is an example of what I am saying. The error in the print screen is made intentionally by choosing an non existing file to show you that even when there is an error, the ErrorLevel is equal to 0.

The command I am using is the following (I tried $LastExitCode and $ExitCode):

powershell -NonInteractive -NoProfile -Command "(Get-Content HvsvmAppserver.ref) -replace '-Dfr.base0.role=', '-Dfr.base0.role=USR_ROLE'| Set-Content -encoding ASCII HvsJvmAppserver.ref; exit $LastExitCode"

But I also tried the same with:

powershell -NonInteractive -NoProfile -Command "(Get-Content HvsvmAppserver.ref) -replace '-Dfr.base0.role=', '-Dfr.base0.role=USR_ROLE'| Set-Content -encoding ASCII HvsJvmAppserver.ref; exit $ExitCode"

Based on the comment of majkinetor, I tried the following and hasn't work as well :(, I am keep getting ErrorLevel 0 in Dos even when an error occurs in powershell.

powershell -noprofile -command " try { (Get-Content HvsvmAppserver.ref) -replace '-Dfr.base0.role=', '-Dfr.base0.role=USR_ROLE'| Set-Content -encoding ASCII HvsJvmAppserver.ref } catch {exit $LastExitCode}"

You can see clearly an error related to the file name that it doesn't exist, but the error level is always 0.

What I want is to get an error code other than the 0 for each error that happens while executing the PowerShell command.

Error 2

Overeat answered 29/4, 2016 at 16:27 Comment(11)
Don't run powershell.exe ... from a cmd.exe prompt. Just run the commands directly from the PS C:\> PowerShell prompt.Residuum
Neither of your images were necessary (you can copy text from a command window), and they certainly could have been cropped to not be so large. None of the black blank area is relevant or necessary, and anyone reading this post on a mobile device is not happy with you. Images should only be posted when there is no other way to illustrate the issue. See this Meta post for a list of the many reasons you should avoid them.Tetzel
@Bill_Stewart, when we ask for a help, it's not to get an answer about something else...your comment wasn't helpfull at all..if I wanted to make it with a different way I wouldn't ask my questionOvereat
@KenWhite Personnaly when I read a question and want to help, I prefere print screens more than codes only...anyway I will crop the images, but I won't delete them...thanks anywayOvereat
Great. You stand alone. I gave you a long list of the many reasons this site does not prefer images, and apparently you didn't read it. The biggest reason to avoid code in images is that people can't copy and paste it to use to try to help, and most are not willing to retype it all. So if you want to reduce (or at least delay) your chances of getting help, keep posting code in images.Tetzel
@KenWhite, thanks again ! it seems that you haven't check well my post !! I already put code that people can copy and past and on the same time I illustrated things by print screens....I agree with you that if we don't put code people can't copy and past, but print screens are helpfull to show exactly what we mean especially if we are trying to express ourselves in a langage which is not ours so that maybe with pictures people will understand us more.Overeat
Sometimes a thing being asked about is not the best way to go about doing a certain thing.Residuum
@Residuum You can't judge when you don't know the environnement of the company and asker etc...if you have an answer to a question give it, and if you don't have it's ok, no need to ask the person to do something else...if i wanted or could do it with a different way I wouldn't ask my question here .... Thanks for respecting people's question. I know why I am asking to do it that way, and am not into writing 5 pages to explain the why, i tried to go directly to the target and ask a direct question..Overeat
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.Blucher
@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.Overeat
The fact that you don't yet have a working solution suggests (at least, to me) that your methodology may be lacking. You may want to try an alternative approach.Residuum
C
9

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.GetContentCommand

C:> 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.

Carolinecarolingian answered 29/4, 2016 at 17:14 Comment(8)
Thanks a lot, I will try that tomorrow at work and will let you know if it solved or not my problem.Overeat
Dunno if I got it well, but I tried and keep getting the Error Level 0 ... here is the command that I used . powershell -noprofile -command " try { (Get-Content HvsvmAppserver.ref) -replace '-Dfr.base0.role=', '-Dfr.base0.role=USR_ROLE'| Set-Content -encoding ASCII HvsJvmAppserver.ref } catch {exit $LastExitCode}"Overeat
I even tried this (even if i don't want a cuztom error) and i keep getting errorlevel 0 .... Dunno the ones who voted +2 to your answer if they tested it or just voted without even testing... powershell -noprofile -command " try { (Get-Content HvsvmAppserver.ref) -replace '-Dfr.base0.role=', '-Dfr.base0.role=USR_ROLE'| Set-Content -encoding ASCII HvsJvmAppserver.ref } catch {exit 12345}"Overeat
You didn't do it right. Do not return with exit $LastExitCode, it is always 0 in your case. Since you used posh cmdlet it doesn't return errorlevel at all but error object $_. Use exit 1 for example.Carolinecarolingian
as you see in the code just above your comment I am not using $LastExitCode, I put exit 12345 and i still get 0 !Overeat
That means that there was no exception. Add write-host 123 before exit to make sure.Carolinecarolingian
thanks a lot for trying to help.....I confirm your version with adding -ea stop, allows to receive the 123 while doing the echo %ErrorLevel%, but there is something wrong in your code because the replace itself is not working, while before the replace was working but the error wasn't detected...... Also, I would like to understand do you think that there is a way to catch an automatic error or it should be a personnalized one ? In addition, can you please clarify me what is the intention of the solution with $ErrorActionPreferance ? it's just to avoid putting -ea stop ? 10x againOvereat
Adding -Raw parameter to gc might help. $EAPref is to avoid -ea stop. You must use personalized error with posh cmdlets or LastExitCode for native exe's.Carolinecarolingian
M
1

Consider using the option -file instead of -command. By using -file option, you will get any value, you have specified in your code, while the -command option will provide you either 0, or 1.

Try this scenario:

> cmd.exe

> cd %userprofile%\desktop
> echo $temp=999;write-host $temp;exit $temp > test.ps1
> echo %errorlevel%
0

> powershell .\test.ps1
999

> echo %errorlevel%
1

> powershell -command .\test.ps1
999

> echo %errorlevel%
1

And now ...

> powershell -file .\test.ps1
999

> echo %errorlevel%
999

Good luck!

Mudstone answered 4/11, 2020 at 21:28 Comment(0)
B
0

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.

Blucher answered 2/5, 2016 at 14:35 Comment(5)
@Shwan Melton, thanks a lot. Now I understand why I have always 0. Still the second part that I don't understand, is why if I put exit 99, I won't get it 99 ? would you please clarify me again the second part of your answer ? As for why I ma doing that through dos, it's because at my company they have very long .cmd, and If I want to modify anything I am obliged to use this .cmd and can't write external scripting etc...Othwerwise I could have done what I want using a .vbs, but the rules we have are complicated and I can't do all I want, am obliged to do it in the unique .cmdOvereat
Executing PowerShell scripts via command prompt (DOS) does not guarantee that you can change the exit code to be anything other than zero.Blucher
So to conclude I can't do what I want, or to make it clear, I can do it, but I can't guarantee to manage potential errors...did I get it well this time ? :)Overeat
I am sure this is wrong - Continuous build depends on powershell returning something other then 0 when things fail in order to stop the build. I have used this approach number of times and it works. The example I have given was tested on Win7,8,10 with Powershell 4,5.Carolinecarolingian
Actually exit is documented as setting ErrorLevel: learn.microsoft.com/en-us/powershell/module/…Undercover
M
0

If you use an external command last, it will set the errorlevel. I'm quoting the pipe with a "^".

powershell echo hi ^| findstr hi2

Or you can test $? and exit with a nonzero number:

powershell 1/0; if (! $?) { exit 1 } 

Actually if the LAST cmdlet raises an error, terminating or not, it should set the cmd errorlevel.

You can do something like this, and exit with the number of errors made.

powershell $errorcount = $error.count; 1/0; echo hi; exit $error.count - $errorcount

Script terminating errors will set the errorlevel:

powershell throw 'hi'

Thinking more about the original problem. If you don't use the parentheses, the errorlevel will get set if it can't find the file, since the current pipeline will have an exception. But the output file would have to be different. Running it as a script wouldn't give the same result (neither would unix).

powershell "get-content foo2 | foreach { $_ -replace 'a','b' } | set-content foo3"

get-content : Cannot find path 'C:\Users\admin\foo2' because it does not exist.
At line:1 char:1
+ get-content foo2 | foreach { $_ -replace 'a','b' } | set-content foo3
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\admin\foo2:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand


echo %errorlevel%

1
Mephistopheles answered 4/6, 2019 at 15:12 Comment(0)
B
0

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.

Beauvais answered 28/8, 2019 at 16:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.