Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent TFS from failing a PowerShell step when an error is written to the error stream?

I have a PowerShell step in a TFS build that writes an error to the error stream and thus fails the build. I'd like the step to ignore the error and continue on as succeeded.

Process completed with exit code 0 and had 1 error(s) written to the error stream.

I've tried setting the Continue on error option in TFS, but this results in a "build partially succeeded" status, but what I want is for it to be "successful".

I've also tried adding 2>&1 > output.txt to the end of the line that generates the error, but the errors are still written to the TFS output and not captured in the text file.

The command I'm executing is the New-TfsChangeset cmdlet from the TFS 2015 Power Tools.

like image 920
John Mills Avatar asked Jun 15 '17 06:06

John Mills


People also ask

How do I stop a PowerShell script from error?

Run the PowerShell script file by running the command . \text. ps1 as shown in the below screenshot. Notice that instead of PowerShell closing the session, PowerShell terminates the script and returns control to the prompt of the current PowerShell session.

How do you ignore an error in PowerShell and let it continue?

If there are special commands you want to ignore you can use -erroraction 'silentlycontinue' which will basically ignore all error messages generated by that command. You can also use the Ignore value (in PowerShell 3+): Unlike SilentlyContinue, Ignore does not add the error message to the $Error automatic variable.

How do you catch error messages in PowerShell?

You can use Get-Error to display a specified number of errors that have occurred in the current session using the Newest parameter. The Get-Error cmdlet also receives error objects from a collection, such as $Error , to display multiple errors from the current session.

How do I raise exceptions in PowerShell?

To create our own exception event, we throw an exception with the throw keyword. This creates a runtime exception that is a terminating error. It's handled by a catch in a calling function or exits the script with a message like this.


2 Answers

You could uncheck the Fail on Standard Error in your PowerShell script configuration and write the lastexitcode to pass the task.

Enter image description here

Fail on Standard Error

If this is true, this task will fail if any errors are written to the error pipeline, or if any data is written to the Standard Error stream. Otherwise the task will rely solely on $LASTEXITCODE and the exit code to determine failure.

Then you can output the error or warning by using PowerShell or VSTS task commands.

Write-Warning “warning”
Write-Error “error”
Write-Host " ##vso[task.logissue type=warning;]this is the warning"
Write-Host " ##vso[task.logissue type=error;sourcepath=consoleapp/main.cs;linenumber=1;columnnumber=1;code=100;]this is an error "

More information about the VSTS task command, you can refer to: Logging Commands

like image 55
PatrickLu-MSFT Avatar answered Oct 23 '22 15:10

PatrickLu-MSFT


The following script worked for me in VSTS:

Write-Host "Running build script..."
&$CAKE_EXE $cakeArguments 2>&1 | Write-Host

I just added "2>&1 | Write-Host" to the command so that the standard error stream will be routed to the Write-Host stream.

like image 43
Almogo Avatar answered Oct 23 '22 13:10

Almogo