告诉 PowerShell ISE 不要将 stderr 发送到 Write-Error [英] Tell PowerShell ISE to not send stderr to Write-Error
问题描述
当可执行文件写入标准错误流 (stderr
) 时,PowerShell 控制台和 PowerShell ISE 的行为有所不同.控制台 (powershell.exe) 将其显示为标准输出.例如,当我在非存储库上获得 Mercurial 的状态时,它会写入标准错误:
The PowerShell console and the PowerShell ISE behave differently when executables write to the standard error stream (stderr
). The console (powershell.exe) displays it like standard output. For example, when I get a status with Mercurial on a non-repository, it writes to standard error:
> hg st -R C:\Windows
abort: repository C:\Windows not found!
但是,在 PowerShell ISE (powershell_ise.exe
) 中,该错误会传递给 PowerShell 的 Write-Error
cmdlet:
However, in the PowerShell ISE (powershell_ise.exe
), that error gets passed to PowerShell's Write-Error
cmdlet:
> hg st -R C:\Windows
hg.exe : abort: repository C:\Windows not found!
At line:1 char:3
+ hg <<<< st -R C:\Windows
+ CategoryInfo : NotSpecified: (abort: repository C:\Windows not found!:String) [], RemoteExcepti
on
+ FullyQualifiedErrorId : NativeCommandError
有没有办法将 ISE 配置为像控制台一样运行,而不将 stderr 流发送到 Write-Error
?
推荐答案
hg st -R C:\Windows 2>&1 | %{ if ($_ -is [System.Management.Automation.ErrorRecord]) { $_.Exception.Message } else { $_ } }
这会保留 stderr 输出并将其作为正常输出发送,而不是丢弃它.
This preserves the stderr output and sends it as normal output, rather than dropping it.
这篇关于告诉 PowerShell ISE 不要将 stderr 发送到 Write-Error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!