I have the following cmdlet written in C #, it basically just throws an error:
[Cmdlet("Use", "Dummy")] public class UseDummyCmdlet :PSCmdlet { protected override void ProcessRecord() { var errorRecord = new ErrorRecord(new Exception("Something Happened"), "SomethingHappened", ErrorCategory.CloseError, null); ThrowTerminatingError(errorRecord); } }
I assume (I could be wrong), this is the equivalent in PowerShell)
Function Use-Dummy() { [CmdletBinding()] Param() process { $errorRecord = New-Object System.Management.Automation.ErrorRecord -ArgumentList (New-Object System.Exception), 'SomethingHappened', 'NotSpecified', $null $PSCmdlet.ThrowTerminatingError($errorRecord) } }
The PowerShell version behaves as expected:
Use-Dummy : Exception of type 'System.Exception' was thrown. At line:1 char:10 + use-dummy <<<< + CategoryInfo : NotSpecified: (:) [Use-Dummy], Exception + FullyQualifiedErrorId : SomethingHappened,Use-Dummy
However, the C # version is crashing, with the following information:
An exception of type 'System.Management.Automation.PipelineStoppedException' occurred in System.Management.Automation.dll but was not handled in user code Additional information: The pipeline has been stopped.
What am I doing wrong?
source share