Force Powershell script to Stop but leave the console open to review

This topic contains 2 replies, has 2 voices, and was last updated by  Kawika Moss 3 years, 2 months ago.

  • Author
  • #24264

    Kawika Moss

    I have a form that opens, with powershell, and also the console opens so you can see the verbose output while the forms operations are performed, based on what button is clicked. And if an error occurs that powershell recognizes, it stops as expected. But if something in my logic errors, the script continues to run as if nothing happened, so I'm trying to figure out how I can throw an error and stop the script from running without actually closing the console.

    EXIT doesn't seem to be the correct choice, because it will close the console, is there an alternate solution?

  • #24265

    Dave Wyatt

    Typically you'll do this with a "throw" statement. This produces a terminating error that will abort your script, unless your code handles it with a try/catch block somewhere.

  • #24268

    Kawika Moss

    Nice, I was just testing with using -ErrorAction Stop like this:

    Write-Error -Message "`$CopyDirection value must be equal to `"ToNetwork`" or `"ToLocal`"" -ErrorAction Stop

    That seemed to work ok, but I like the Throw one too.

    What is the difference, is one more commonly used then the other, is one more recommended than the other?

You must be logged in to reply to this topic.