我在一个PowerShell ISE运行完全相同script.ps1文件(手动加载脚本,按F5),并在PowerShell控制台(执行脚本文件)。 他们都工作,但ISE显示错误讯息控制台没有。 为什么?
该代码是:
git push origin master
Write-Host "lastExitCode: $lastExitCode Last command was successful: $?"
这段代码输出这个错误在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
而这在控制台:
Everything up-to-date
lastExitCode: 0 Last command was successful: True
你可以看到,成功的地位是不是也一样。
我不知道他们为什么输出不同,但我们从看到消息git push
是过来标准错误。 这意味着,它们都显示错误,虽然ISE是使他们更响亮,并将之转化为错误的对象 。
考虑从PowerShell提示符的输出:
PS> git push
Everything up-to-date
PS> git push 2> $null # Redirect stderr to $null
PS> $LastExitCode
1
PS>
并且比较它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>
除了从误差对象额外输出被显示的,所述输出是相同的。 在ISE已经转换标准错误字符串到NativeCommandError对象,如果你看看过去的红色它甚至显示错误消息。
在所示这个答案#1 ,以防止git push
打印到STDERR的解决方法是调用命令WITN --porcelain
选项。
然后,调用
git push origin master --porcelain
输出变为所有STDOUT
所以,下面的情况下例如具有任何错误,则该命令-q 2>&1 | %{ “$ _”}`会抵消错误的结果。
的溶液,并使用: git push -q 2>&1 | %{ "$_" }
git push -q 2>&1 | %{ "$_" }
嗯,我能想到的右把我的头顶部的唯一的主要区别是,ISE使用v2的单线程单元(STA)模式和控制台使用多线程单元(MTA)。 您是否尝试运行powershell.exe与-STA说法,或-MTA powershell_ise.exe,再试的脚本?
它看起来像错误是当您试图运行,FWIW Git的命令来了。
文章来源: Why is Powershell ISE showing errors that Powershell console does not show?