Wie kann ich in PowerShell eine "nackte" Fehlermeldung ohne zugehöriges Stacktrace anzeigen?
Wie kann ich von PowerShell nach stderr schreiben oder Fehler abfangen, so dass
Error-Meldung wird als Fehler angezeigt (Schreiben an stderr, sodass TeamCity und Octopus dies als Fehler ansehen)No Stack Trace Müll verwirrt meine schöne, prägnante FehlermeldungAlle diese Jahre habe ich durch @ überlethrow
ing Fehler oder Schreiben überWrite-Error
, aber ich bin müde und alt und möchte in meinen Skripten nur eine kurze Fehlermeldung sehen. Ich habe jede Kombination von @ ausprobietrap
, throw
, Write-Error
, und-ErrorAction
, umsonst
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?"
}
Hier ist die Benutzererfahrung, die ich sehen möchte:
C:\> & .\Do-Something.ps1
An error occurred attempting to 'do something.' Have you tried rebooting?
C:\> ▏
Stattdessen bekomme ich:
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:\> ▏