03 / RELIABLE ADMINISTRATION TOOLS · LESSON 12

Keep partial failures visible

Return one explicit outcome per requested host while continuing past unreadable snapshots.

12 min · learn at your pace

One failure should not erase the rest

Fleet work often succeeds for some systems and fails for others. This lesson models that situation with local snapshot files. It does not contact a remote computer or simulate WSMan; OS and remoting simulations are planned for the shared Advanced PowerShell path.

Define a function that reads one named JSON file per requested host. Put try / catch inside the loop, so a missing or malformed file creates one failed result and the next host is still processed. A catch around the entire loop would abandon the remaining work.

Make the output contract explicit

Each result contains Computer, Success, MemoryGiB, and Error. Successful snapshots have a positive numeric TotalMemoryBytes and a Computer matching the requested name. Failed snapshots return false, null memory, and a non-empty diagnostic. A mismatched identity is a failure even if its JSON parses successfully.

Get-Content '/home/user/snapshots/srv-web01.json' -Raw | ConvertFrom-Json

Use -ErrorAction Stop on file reading and JSON parsing to make expected input problems catchable. Keep diagnostics in the Error property instead of emitting them as extra success-pipeline strings. That preserves one result per host, in request order.

The names in this exercise are supplied safe identifiers, not arbitrary path fragments. A production reader accepting untrusted names would also need explicit path-scope validation. Missing data must never become a fabricated zero-memory “healthy” machine.

Try an example.ps1
Get-Content '/home/user/snapshots/srv-web01.json' -Raw | ConvertFrom-Json
YOUR TURN

Put it into practice.

Define Read-HostSnapshots -Names <string array> -Directory. Read <name>.json for each requested host and return Computer, Success (boolean), MemoryGiB, Error in request order. A valid snapshot matches Computer and has positive numeric TotalMemoryBytes. Missing files, malformed JSON, mismatched names, and invalid capacity must produce Success false, MemoryGiB null, and a non-empty Error, then continue. Empty Names returns no results.

See an explained solution
One way to solve it.ps1
function Read-HostSnapshots {
    param([string[]]$Names, [string]$Directory)
    foreach ($name in $Names) {
        try {
            $path = Join-Path $Directory ($name + '.json')
            $snapshot = Get-Content -LiteralPath $path -Raw -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
            if ($snapshot.Computer -ne $name -or ($snapshot.TotalMemoryBytes -isnot [long] -and $snapshot.TotalMemoryBytes -isnot [int] -and $snapshot.TotalMemoryBytes -isnot [double]) -or $snapshot.TotalMemoryBytes -le 0) { throw 'Invalid snapshot identity or capacity.' }
            [pscustomobject]@{ Computer=$name; Success=$true; MemoryGiB=$snapshot.TotalMemoryBytes / 1GB; Error='' }
        }
        catch { [pscustomobject]@{ Computer=$name; Success=$false; MemoryGiB=$null; Error=$_.Exception.Message } }
    }
}
Read-HostSnapshots -Names @('srv-web01','missing-host','broken-host','srv-db01') -Directory '/home/user/snapshots'

Per-host error handling retains useful successes while exposing incomplete coverage. Identity and positive-capacity validation prevent a parseable but unsuitable document from becoming a false success.