04 / SHIP DEPENDABLE FLEET TOOLS · LESSON 16

Export repeatable, labelled reports

Write deterministic CSV results and a JSON summary that identifies the simulated data.

16 min · learn at your pace

Outputs are part of the tool's interface

A script that works once in the terminal still needs a dependable reporting boundary. Export endpoint observations to CSV and a small summary to JSON. Consumers should not need to scrape Format-Table output or infer which run appended each row.

Sort by ComputerName and numeric Port before exporting. Overwrite the two report files on each run so the same input produces the same records. Preserve every endpoint status. Count TimedOut and Error as Unknown, and count Open separately.

Label provenance

The summary includes Simulation=true. This distinguishes the browser's fixture-backed results from observations collected on a real network. The flag describes the source, not a judgement about whether the learner's PowerShell code is real.

The empty-input case still writes valid files: a CSV header with no data rows and a JSON summary with zero totals. Without that rule, an old report can survive a new empty run and appear current. Create the requested output directory if necessary.

Preserve the source and choose the destination

This function only consumes result objects and writes into its requested output directory. It does not change the fleet, run probes, or append to its inputs. Keeping reporting separate from collection makes it easier to test with tiny datasets. In production, review filesystem permissions and publication policy for the destination; this exercise writes only the lab's virtual filesystem.

Try an example.ps1
$row=[pscustomobject]@{ComputerName='app01';Port=443;Status='Open';ElapsedMs=12;Detail='Fixture observation'}
$row | ConvertTo-Json
YOUR TURN

Put it into practice.

Define Export-FleetReport(Result object[], OutputDirectory). Create the directory, overwrite ports.csv sorted by ComputerName/Port with the five observation columns, and write summary.json containing Simulation=true, Total, Open, and Unknown (TimedOut + Error). Return that summary object. Empty input must overwrite stale data with a header-only CSV and zero counts.

See an explained solution
One way to solve it.ps1
function Export-FleetReport {
    param([object[]]$Result,[string]$OutputDirectory)
    New-Item -ItemType Directory -Path $OutputDirectory -Force | Out-Null
    $rows=@($Result | Sort-Object ComputerName,Port)
    $rows | Export-Csv (Join-Path $OutputDirectory 'ports.csv') -NoTypeInformation
    if($rows.Count -eq 0) { Set-Content (Join-Path $OutputDirectory 'ports.csv') '"ComputerName","Port","Status","ElapsedMs","Detail"' }
    $summary=[pscustomobject]@{Simulation=$true;Total=$rows.Count;Open=@($rows | Where-Object Status -EQ Open).Count;Unknown=@($rows | Where-Object { $_.Status -in @('TimedOut','Error') }).Count}
    $summary | ConvertTo-Json | Set-Content (Join-Path $OutputDirectory 'summary.json')
    $summary
}

Each export replaces the previous run, including empty results. The summary preserves uncertainty and explicitly labels the source as a simulation.