04 / RELIABLE NETWORK AUTOMATION · LESSON 15

Keep missing network exports visible

Read one saved export per device and preserve partial failures without inventing healthy results.

12 min · learn at your pace

Process each expected device independently

This lesson reads already-collected files, not remote devices. For each requested DeviceId, load Directory/DeviceId.json. Put try/catch inside the loop so a missing or malformed file becomes one failed result while later devices are still assessed.

Use -ErrorAction Stop on file reading and JSON conversion. Validate that the export's DeviceId matches the requested device and Ports is a JSON array. A valid file belonging to another device must not satisfy the request.

Get-Content '/home/user/exports/D01.json' -Raw | ConvertFrom-Json

Empty is different from unavailable

A successful export with Ports as an empty array has PortCount 0. A missing, malformed, wrong-identity, or wrong-shaped export has Success false, null PortCount, and a diagnostic. Substituting zero for failure would make collection gaps look like a real empty inventory.

Emit exactly one object per requested device in request order. Keep diagnostics in Error rather than emitting extra success-pipeline strings that a caller might mistake for rows.

The supplied device IDs are safe fixed identifiers; this lesson does not accept arbitrary user-controlled path fragments. The export contract covers already-normalised port rows, so this function validates the envelope and counts them rather than auditing every configuration field. The next lesson reports changes and collection gaps separately.

Try an example.ps1
Get-Content '/home/user/exports/D01.json' -Raw | ConvertFrom-Json
YOUR TURN

Put it into practice.

Define Read-NetworkExport -DeviceIds -Directory. For each supplied safe device ID, read its JSON file, require matching DeviceId and a Ports array, and emit DeviceId, boolean Success, PortCount, Error in request order. Valid empty arrays give count 0. Any read/parse/envelope failure gives false, null count, and a non-empty error without stopping later devices. Accept an empty ID array.

See an explained solution
One way to solve it.ps1
function Read-NetworkExport {
    param([AllowEmptyCollection()][string[]]$DeviceIds,[string]$Directory)
    foreach ($id in $DeviceIds) {
        try {
            $path = Join-Path $Directory ($id + '.json')
            $data = Get-Content -LiteralPath $path -Raw -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
            if ($data.DeviceId -ne $id -or $data.Ports -isnot [array]) { throw 'Export identity or Ports array is invalid.' }
            [pscustomobject]@{ DeviceId=$id; Success=$true; PortCount=$data.Ports.Count; Error='' }
        } catch {
            [pscustomobject]@{ DeviceId=$id; Success=$false; PortCount=$null; Error=$_.Exception.Message }
        }
    }
}

One try/catch per device preserves the successful later export. Explicit identity and array checks prevent a different device or malformed envelope from appearing as a valid zero-port inventory.