如何在PowerShell中显示“裸”错误消息而不附带堆栈跟踪? [英] How can I display a 'naked' error message in PowerShell without an accompanying stacktrace?
问题描述
- 错误消息显示为错误(真正写入到
- 没有堆栈跟踪垃圾混乱我的美丽,简洁的错误信息
所有这些年,我幸存下来的抛出
错误或通过 ,但我累了,老了,在我的脚本中,我只想看一个简明的错误信息。我一直在尝试
陷阱
, throw
, Write-Error
和 -ErrorAction
,无效:
尝试{
抛出错误#Sample代码为StackOverflow。在剧院
#你想像中,有代码在这里做一些真正有用的东西
} catch {
写错误尝试做某事发生错误。尝试重新启动?
}
以下是我想看到的用户体验:
C:\> &安培; .\Do-Something.ps1
尝试做某事发生错误。您尝试重新启动吗?
C:\> ▏
相反,我得到:
C:\> &安培; .\Do-Something.ps1
尝试做某事发生错误。您尝试重新启动吗?
在行:1 char:1
+ Do-RealWork
+ ~~~~~~~~~~~~~~~~~~~~
+类别信息: NotSpecified:(:) [Write-Error],WriteErrorException
+ FullyQualifiedErrorId:Microsoft.PowerShell.Commands.WriteErrorException,Do-RealWork
C:\> ▏
设置自动 $ ErrorView
变量为'CategoryView'
会导致PowerShell输出简洁的单行错误表示,但是这种表示可能并不总是包括足够的信息,因为错误消息通常不包括 反之,文本传递给投掷...反映,但相反,
写-Error
输出包含否具体信息,而'CategoryView'
正在生效。
添加PowerShell的一个新错误视图是单行的,但始终包含所有关键信息。正在为v6进行讨论
如果您的PowerShell代码是从控制台 一个控制台主机),使用 [Console] :: Error.WriteLine()
,其中 无条件到外界 (标准错误流):
[Console] :: Error.WriteLine(发生错误...您是否尝试重新启动?)
注意:
-
这不会从非控制台主机如PowerShell ISE。
-
[Console] :: Error.WriteLine()
output doesn
令人遗憾的是,在PowerShell(跨主机)和之间的之间没有单一的解决方案。 :
-
[控制台] :Error.WriteLine()
,在正确写入外部世界的stderr时,不能在PowerShell内捕获或抑制其输出 -
同样,
$ host.ui.WriteErrorLine()
,尽管与所有主机配合使用,但它是一种在PowerShell流系统外部工作的 UI 方法,因此也不能捕获或抑制其输出在PowerShell中。
更重要的是,它不写入外界的stderr(它b在这方面,像Write-Error
一样,见下文。 -
em> PowerShell,只有
写入错误
写入PowerShell的错误流,因此可以捕获/禁止其输出 。
然而,不幸的是, c(除了嘈杂之外)不写 写入外界的stderr,除非奇怪的是,stderr被明确地被重定向了 - 有关详细信息,请参阅我的这个答案。 p>
[1]彼得(OP自己)提供了一个解决方法:
[Console] :: ForegroundColor ='red'
[Console] :: Error.WriteLine(An发生错误...您是否尝试重新启动?)
[控制台] :: ResetColor()
suneg的有用答案为其提供了一个函数包装器。
幸运的是,当PowerShell检测到输出被重定向到文件时,PowerShell会自动省略颜色代码。
How can I write to stderr from PowerShell, or trap errors such that
- Error message is displayed as an error (truly writing to stderr so that TeamCity and Octopus see it as an error)
- No stack trace garbage muddles my beautiful, concise error message
All these years I've survived by throw
ing errors or writing via Write-Error
, but I'm tired and old, and in my scripts I just want to see one concise error message. I've been trying every combination of trap
, throw
, Write-Error
, and -ErrorAction
, to no avail:
try {
throw "error" #sample code for StackOverflow. In the theater
#of your mind, imagine there is code here that does something real and useful
} catch {
Write-Error "An error occurred attempting to 'do something.' Have you tried rebooting?"
}
Here's the user experience I want to see:
C:\> & .\Do-Something.ps1
An error occurred attempting to 'do something.' Have you tried rebooting?
C:\> ▏
Instead I get:
C:\> & .\Do-Something.ps1
An error occurred attempting to 'do something.' Have you tried rebooting?
At line:1 char:1
+ Do-RealWork
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Do-RealWork
C:\> ▏
Setting the automatic $ErrorView
variable to 'CategoryView'
causes PowerShell to output concise, single-line error representations instead, but this representation may not always include enough information, because the error message is typically not included; on the plus side, the text passed to Throw "..."
is reflected, but, by contrast, Write-Error
output contains no specific information while 'CategoryView'
is in effect.
Adding a new error view to PowerShell that is single-line yet always contains all crucial information is being discussed for v6.
Provided that your PowerShell code is run from a console (uses a console host), use [Console]::Error.WriteLine()
, which unconditionally writes to the outside world's stderr (standard error stream):
[Console]::Error.WriteLine("An error occurred ... Have you tried rebooting?")
Note:
This won't work from non-console hosts such as the PowerShell ISE.
[Console]::Error.WriteLine()
output doesn't print in red in the console [1].
Sadly, there is no single solution that works both from within PowerShell (across hosts) and from outside of it:
[Console]::Error.WriteLine()
, while writing properly to stderr for the outside world, cannot have its output captured or suppressed inside PowerShell, and only works with the PowerShell console host.Similarly,
$host.ui.WriteErrorLine()
, even though works with all hosts, it is a UI method that works outside PowerShell's stream system as well and therefore its output too cannot be captured or suppressed in PowerShell.
More importantly, it doesn't write to the outside world's stderr (it behaves likeWrite-Error
in this respect, see below).Inside PowerShell, only
Write-Error
writes to PowerShell's error stream, so its output can be captured / suppressed.
However, unfortunately,Write-Error
(apart from being noisy) does not write to the outside world's stderr, unless, bizarrely, stderr is explicitly being redirected - see this answer of mine for details.
[1] Peter (the OP himself) offers a workaround for that:
[Console]::ForegroundColor = 'red'
[Console]::Error.WriteLine("An error occurred ... Have you tried rebooting?")
[Console]::ResetColor()
suneg's helpful answer provides a function wrapper for it.
Fortunately, PowerShell automatically omits the color codes when it detects that the output is being redirected (to a file).
这篇关于如何在PowerShell中显示“裸”错误消息而不附带堆栈跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!