Search code examples
powershelltfstf-cli

How to handle tf.exe errors in PowerShell script


I have a PowerShell script that run tf.exe commands.

$tfExe = {C:\path\to\tf.exe}
& $tfExe checkout / checkin etc.

If I try to run checkin command to file that not changed I got an error:

The following changes were not checked in because the item were not modified.

Undoing edit: {C:\path\to\file}

The problem is, I run the script within a TFS build, and the build fail with an error:

[error] There are no remaining changes to check in.

But this is not a real error, if there is no changes so don't do check in, this is good behavior for me.

How can I handle the tf.exe errors?

I tried with try catch but is not worked, altough there is an error the try block is excuted and not the catch.

I tried also with a variable get the output $test = & $tfExe checkin ... and still get an error (and the variable is empty).


Solution

  • PowerShell doesn't understand error from native executables. You have to parse through the output to take decision. You can redirect the error stream to output stream.

    $Output = tf.exe … 2>&1
    
    $Output.exception.message -match 'There are no remaining'