Why is Powershell ISE showing errors that Powershell console does not show?

会有一股神秘感。 提交于 2019-12-05 13:22:36

问题


I'm running exactly the same script.ps1 file in a Powershell ISE (manually loading the script and pressing F5) and in a Powershell console (executing the script file). They both work but ISE shows errors that the console does not. Why?

The code is:

git push origin master
Write-Host "lastExitCode: $lastExitCode Last command was successful: $?"

This code output this error in the ISE:

git.cmd : Initializing to normal mode
At E:\script.ps1:28 char:4
+ git <<<<  push origin master
    + CategoryInfo          : NotSpecified: (Initializing to normal mode:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Initializing to normal mode

Everything up-to-date

lastExitCode: 0 Last command was successful: False

And this in the console:

Everything up-to-date
lastExitCode: 0 Last command was successful: True

You can see that the success status is not the same also.


回答1:


I don't know why they output differently, but the message that we see from git push is coming over stderr. This means that they are both showing errors, although the ISE is making them much louder, and converting it into error objects.

Consider this output from the PowerShell prompt:

PS> git push
Everything up-to-date
PS> git push 2> $null    # Redirect stderr to $null
PS> $LastExitCode
1
PS>

and compare it to the ISE:

PS> git push
git : Everything up-to-date
At line:1 char:1
+ git push
+ ~~~~~~~~
    + CategoryInfo          : NotSpecified: (Everything up-to-date:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
PS> git push 2> $null
PS> $LastExitCode
1
PS>

Except for the extra output from the error object being displayed, the output is the same. The ISE has converted the stderr string to a NativeCommandError object, and it does even display the error message if you look past the red.




回答2:


as shown in this Stackoverflow answer to prevent git push to print to STDERR the solution is to call the command witn --porcelain option.

then, calling

git push origin master --porcelain

output goes all to STDOUT




回答3:


So, the example below case have any error , this command -q 2>&1 | %{ "$_" }` will nullifying the result of errors.

A solution and use: git push -q 2>&1 | %{ "$_" }




回答4:


Hmmm, the only major difference I can think of right off the top of my head is that the ISE uses single-threaded apartment (STA) mode in v2, and the console uses multi-threaded apartment (MTA). Have you tried running powershell.exe with the -STA argument, or powershell_ise.exe with -MTA, and trying the script again?

It looks like the error is coming from the Git command you're trying to run, FWIW.



来源:https://stackoverflow.com/questions/10856609/why-is-powershell-ise-showing-errors-that-powershell-console-does-not-show

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