Option to output a pipeline terminating error via a Write-Error switch parameter
Votes from Connect: 3
Original Date Submitted: 4/14/2015 6:32:46 PM
Handle: Carl Sörqvist
Site Name: PowerShell
Feedback ID: 1247244
There are three types of errors in Powershell:
Non-terminating errors are generated by the Cmdlet.WriteError method in .NET (https://msdn.microsoft.com/en-us/library/system.management.automation.cmdlet.writeerror(v=vs.85).aspx) or by Write-Error/$PSCmdlet.WriteError() in Powershell, and lets both cmdlet/advanced function and script execution continue, unless $ErrorActionPreference is set to "Stop".
Pipeline terminating errors are generated by the Cmdet.ThrowTerminatingError method in .NET (https://msdn.microsoft.com/en-us/library/system.management.automation.cmdlet.throwterminatingerror(v=vs.85).aspx) or by $PSCmdlet.ThrowTerminatingError() in Powershell, and stops the current pipeline from executing any further, but does *not* stop script execution unless $ErrorActionPreference is set to "Stop".
Script terminating errors are generated via the throw keyword both in .NET cmdlets and Powershell functions/scripts and stops all script execution completely.
I'd like the option for Write-Error to generate a terminating error instead of a non-terminating error through a switch parameter (-TerminatePipeline or something) that stops the current pipeline but not the entire script. Should be fairly easy to implement as both the WriteError and ThrowTerminatingError methods takes an ErrorRecord object as a parameter, but what do I know...
Right now, you have to do this in order to throw a pipeline terminating error which is lengthy and annoying:
$Exception = New-Object -TypeName System.ArgumentException -ArgumentList "Something quite terrible happened"
$ErrorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList $Exception, "QuiteTerribleError", "InvalidArgument", $null
# Non-terminating error
Write-Error -Message "Something not so terrible happened"
# Pipeline terminating error
Write-Error -Message "Something quite terrible happened" -TerminatePipeline
# Script terminating error
throw "Something *really* terrible happened"
Product Studio item created by Connect Synchronizer due to creation of feedback ID 1247244 (http://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=1247244).
Internal BugId: 14380