Set-StrictMode turns missing JSON fields into crashes
versions · case
Affectspowershell 51, pwsh 7
Fails asPROPERTYNOTFOUND
Mechanismstrictmode contract
Safe fixpsobject probe
Symptom
Section titled “Symptom”A script parses a JSON manifest and reads optional fields ($entry.tag,
$entry.artifacts.$arch). It works for months — then dies with
PropertyNotFoundException the first time a manifest omits an optional field,
because the script also sets Set-StrictMode -Version Latest.
Set-StrictMode -Version Latest$entry = '{"name":"x"}' | ConvertFrom-Json$entry.tag# PropertyNotFoundException — without StrictMode this quietly yields $nullStrictMode changes the CONTRACT of property access: missing note-properties go from “$null” to “throw”. Optional-field patterns written under default mode become latent crashes when someone adds StrictMode later (usually to catch the dq-regex class of bug — the two traps travel together).
Workaround
Section titled “Workaround”if ($entry.PSObject.Properties.Name -contains 'tag') { $entry.tag }Probe PSObject.Properties.Name before dereferencing optional fields; the
referenced fix wraps every optional manifest access this way.