简体   繁体   中英

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

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.

Based on some comments I read, I would like to say that : I want to make it by PowerShell and through Dos command and not with a different way.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). The ones who have an answer are welcome and thanks for them, but the ones that are here just to comment with unhelpfull comments by asking to change what I want, or to delete pics etc...please don't comment except to provide a help and an answer to my question not by proposing something else, I am asking for a help about something specific, kindly don't change the subject to something else.. The idea of this site is to provide help, not to add useless and unhelpfull comments.

错误 2

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.

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. 在此处输入图片说明

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.

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

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM