Powershell Capturing Errors in Try/Catch Blocks

dmlb2000's Avatar


05 May, 2018 04:36 AM

I've been trying to capture a failed command in powershell that is expected to fail. The test is to give bad arguments to the command and see that it fails in the appropriate way. If it does anything else the job should fail. I've been trying to use Try/Catch blocks which work fine in my test scripts I've been using locally. However, putting them into my repository testing pipeline isn't working. The idea is that the script should continue after the last line and do more testing but it doesn't.

    echo "This should fail";
    $hit_exception = False;
    echo $ErrorActionPreference;
    try {
      coverage run --include='uploader_cli/*,CLIUploader.py' -a CLIUploader.py --verbose debug upload --dry-run --instrument 54;
    } Catch {
      $hit_exception = True;
    if ( ! $hit_exception ) { exit -1; };

Full build log https://ci.appveyor.com/project/dmlb2000/pacifica-cli-uploader/build/job/tqhvp1wj913v9g4f

  1. Support Staff 1 Posted by Ilya Finkelshte... on 07 May, 2018 05:09 PM

    Ilya Finkelshteyn's Avatar

    Try using $LastExitCode as described here, but with your logic (fail build if script did not fail). Let us know if this helps.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:


Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac