1

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);
        }
 }
Usman
  • 2,742
  • 4
  • 44
  • 82
  • Don't suppose you could put your entire script in a [TryCatchFinally](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally?view=powershell-6)? – Drew Jul 19 '18 at 12:16
  • Nops, the scripts will run under some specific environment and they might never contains any tryCatch blocks. I have my own C# based commandlets and I want to throw an exception which should never allow further to execute rest of script at all – Usman Jul 19 '18 at 12:19
  • What you're asking is not possible. There is no "uncatchable" exception that your code could throw short of causing a segfault or something like that. – Ansgar Wiechers Jul 19 '18 at 13:19

1 Answers1

3

Set the error action preference to "Stop" at the beginning of your script.

$ErrorActionPreference = "Stop"

Now, even if the script throws a none terminating error, it will stop at once.

I'd suggest you to read An Introduction to Error Handling in PowerShell as well.

TobyU
  • 3,718
  • 2
  • 21
  • 32
  • I have C# commandlets those needed to throw exceptions from C# code and upon this exception, powershell runtime engin should not execute the rest of script – Usman Jul 19 '18 at 12:20
  • So pretty much [If exception -ne $null Break Script or Return?](https://stackoverflow.com/a/23703056/5552766) – Drew Jul 19 '18 at 12:23
  • Nops I don't want to catch exceptions in the script. I am running C# commandlets from Powershell script, if one of those commandlet encounters any problem, that should throw an exception so that whole script execution in runspace gets stoop. – Usman Jul 19 '18 at 12:37
  • @Usman can't you just return an expected value from those C# commands and verify within your PowerShell script if that specific value has been properly returned? If not, stop the script using the "exit" command. – TobyU Jul 19 '18 at 12:40
  • No that's not possible. I just can throw some exception which should stop further powershell script execution. Because those scripts might accidentally can contain some non-run able things and i need to notify the script writer. – Usman Jul 19 '18 at 12:45