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:\> ▏
I needed to solve this problem myself recently so I put together a Write-ErrorMessage function as detailed here: https://intellitect.com/powershell-write-error-without-writing-stack-trace/
Specifically, I leveraged the combination
Building on the idea in a previous answer, you can override the built-in Write-Error cmdlet temporarily with a custom function.
With this you are utilizing the fact that Powershell Functions takes precedence over cmdlets.
https://technet.microsoft.com/en-us/library/hh848304.aspx
This is the most elegant approach I've been able to come up with to both show beautiful and concise error messages, as well as letting TeamCity detect problems easily.
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 toThrow "..."
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):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 like
Write-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:
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).
The best way in my opinion to trap errors in PowerShell would be to use the following:
Here is an example of how to use this properly. Basically test what you are trying to do in PowerShell with different scenarios in which your script will fail.
Here is a typical PowerShell error message:
Next you would get the exception of the error message:
You would setup your code to catch the error message as follows:
Output would look like the following instead of the Powershell standard error in above example:
Lastly, you can also use multiple catch blocks to handle multiple errors in your code. You can also include a "blanket" catch block to catch all errors you haven't handled. Example:
Hope This Helps!