Skip to content

Build Analysis

Roman Kuzmin edited this page Apr 17, 2015 · 7 revisions

The Invoke-Build parameter Result is used to tell where to store build results like invoked task objects, error and warning messages, list of all tasks, and an error that stops a build.

For example, if a build script have tasks which are called safe (job -Safe), then the build may succeed, that is complete without terminating errors, but it still may have some tasks failed. A caller may want to get this information and perform some actions on errors:

# Build
Invoke-Build Test Tests.build.ps1 -Result Result

# Analyse the Errors
if ($Result.Errors.Count) {
    Write-Warning "Some tests failed."
    ...
}
else {
    Write-Host "All tests succeeded."
    ...
}

Caution. The list Errors will continue to exist (so it is safe to use Errors.Count) but objects may change in the future. For better and safer analysis iterate through Tasks and check their Error, see below.


Detailed error analysis

Note that Errors contains raw errors with no related task data. For more detailed error analysis iterate through Tasks and check their Error. In this way in addition to an error there is useful task information as well: Name, InvocationInfo.ScriptName, and InvocationInfo.ScriptLineNumber.

foreach($t in $Result.Tasks) {
    if ($t.Error) {
        "Task '$($t.Name)' at $($t.InvocationInfo.ScriptName):$($t.InvocationInfo.ScriptLineNumber)"
        $t.Error
    }
}

This analysis is especially useful in testing scenarios:

Invoke-Build ** -Safe -Result Result

After testing $Result.Tasks contains data for a detailed test report.


Show all tasks summary

This code snippet shows all tasks summary after the build (even failed).

try {
    # Invoke the build and keep results in the variable Result
    Invoke-Build -Result Result
}
finally {
    # Show task summary information after the build
    $Result.Tasks | Format-Table Elapsed, Name, Error -AutoSize
}

Show task durations and script names

This code snippet shows all tasks ordered by the Elapsed times and adds task ScriptNames to task names (this is useful if different scripts have same task names, e.g. typical: Build, Clean, Test, ...).

# Invoke the build and keep results in the variable Result
Invoke-Build -Result Result

# Show invoked tasks ordered by Elapsed with ScriptName included
$Result.Tasks |
Sort-Object Elapsed |
Format-Table -AutoSize Elapsed, @{
    Name = 'Task'
    Expression = {$_.Name + ' @ ' + $_.InvocationInfo.ScriptName}
}