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.

  2. Ilya Finkelshteyn closed this discussion on 25 Aug, 2018 02:28 AM.

Comments are currently closed for this discussion. You can start a new one.

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