Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if error from dotnet build command with powershell

I want custom script when my dotnet build command failed. How to get errors list or not success flag when the 'dotnet build' failed with errors compilation for example, or when 'dotnet test' failed, with powershell.

I want equivalent code

dotnet test
if (!$result.Successful) {
    $errorMessage = ""
    if ($result.Error -ne $null) {
        $errorMessage = $result.Error.Message
    }
    Write-Host "##vso[task.logissue type=error;] error"
    Write-Host "##vso[task.complete result=Failed;]"
    Exit -1
}
like image 309
ArDumez Avatar asked Dec 26 '16 21:12

ArDumez


People also ask

What is the DotNet build command?

The dotnet build command builds the project and its dependencies into a set of binaries. The binaries include the project's code in Intermediate Language (IL) files with a .dll extension. Depending on the project type and settings, other files may be included, such as:

Why can't I run DotNet build on another machine?

For executable projects targeting versions earlier than .NET Core 3.0, library dependencies from NuGet are typically NOT copied to the output folder. They're resolved from the NuGet global packages folder at run time. With that in mind, the product of dotnet build isn't ready to be transferred to another machine to run.

How to learn about an error in Windows PowerShell?

Windows PowerShell offers you few ways to learn about an error. The first one I would like to mention is the Error object. Because Windows PowerShell is so very much in love with objects, even an error that a code or a cmdlet encounters is stored in an object.

How do I run a DotNet test from a project?

Examples 1 Run the tests in the project in the current directory: .NET Core CLI dotnet test 2 Run the tests in the test1 project: .NET Core CLI dotnet test ~/projects/test1/test1.csproj 3 Run the tests in the project in the current directory, and generate a test results file in the trx format: .NET Core CLI dotnet test --logger trx More items...


1 Answers

You can merge the standard output and error streams with the > stream redirection operator, and then inspect the $LASTEXITCODE automatic variable to see if the call succeeded or not:

# Merge all streams into stdout
$result = dotnet test *>&1

# Evaluate success/failure
if($LASTEXITCODE -eq 0)
{
    # Success
}
else
{
    # Failed, you can reconstruct stderr strings with:
    $ErrorString = $result -join [System.Environment]::NewLine
}
like image 100
Mathias R. Jessen Avatar answered Oct 06 '22 03:10

Mathias R. Jessen