- 错误消息以错误的形式显示(真正地将其写入标准错误以便TeamCity和Octopus将其视为错误)
- 没有堆栈跟踪垃圾混淆我的简洁错误消息
throw
错误或通过Write-Error
编写信息来生存,但我已经累了,老了,在我的脚本中,我只想看到一个简洁的错误消息。我尝试了所有的组合,包括trap
、throw
、Write-Error
和-ErrorAction
,但都无济于事:try {
throw "error" # Sample code for a stack overflow. 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?"
}
这是我希望看到的用户体验:
C:\> & .\Do-Something.ps1
An error occurred attempting to 'do something.' Have you tried rebooting?
C:\> ▏
相反我得到:
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:\> ▏
$error[0].Exception.Message
包含了错误信息,但我不确定这对你有用。 - sodawillow