I need to terminate the whole script execution if command let encounters the error. The whole script after that must never execute. In my case, If I use ThrowTerminatingError, it just stops the current commandlet to be executed further and the rest of the script executes which I don't want.
I already used "PipelineStoppedException" but this doesn't tell anything in the error stream and no exception can be a catch. Because Invoke call successfully terminates.
How can I achieve this? I want to terminate the whole script execution with the exception message. Following is the C# code which I am using from commandlet but it is still allowing rest of scripts to continue execution further.
Edit
protected override void BeginProcessing()
{
base.BeginProcessing();
if (SomeLogicTrue())
{
var errorRecord = PowershellErrorHandler.GetErrorRecord(new PipelineStoppedException("access is not supported for this context."), "InvalidExecutionContext");
ThrowTerminatingError(errorRecord);
}
}