为什么Powershell ISE显示Powershell控制台未显示的错误?

Mal*_*tre 13 powershell powershell-ise powershell-2.0 powershell-1.0

我在Powershell ISE(手动加载脚本并按F5)和Powershell控制台(执行脚本文件)中运行完全相同的script.ps1文件.它们都有效,但ISE显示控制台没有的错误.为什么?

代码是:

git push origin master
Write-Host "lastExitCode: $lastExitCode Last command was successful: $?"
Run Code Online (Sandbox Code Playgroud)

此代码在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
Run Code Online (Sandbox Code Playgroud)

这在控制台中:

Everything up-to-date
lastExitCode: 0 Last command was successful: True
Run Code Online (Sandbox Code Playgroud)

您可以看到成功状态也不一样.

Rya*_*ert 11

我不知道他们为什么输出不同,但我们看到的信息git push正在传递给stderr.这意味着它们显示错误,尽管ISE使它们声音更大,并将其转换为错误对象.

从PowerShell提示符处考虑此输出:

PS> git push
Everything up-to-date
PS> git push 2> $null    # Redirect stderr to $null
PS> $LastExitCode
1
PS>
Run Code Online (Sandbox Code Playgroud)

并将其与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>
Run Code Online (Sandbox Code Playgroud)

除了显示错误对象的额外输出外,输出相同.ISE已将stderr字符串转换为NativeCommandError对象,如果您看过红色,它甚至会显示错误消息.