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
}
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:
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.
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.
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...
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With