diff --git a/playbook/scripts/preflight.ps1 b/playbook/scripts/preflight.ps1 index a6ab3b3..825f006 100755 --- a/playbook/scripts/preflight.ps1 +++ b/playbook/scripts/preflight.ps1 @@ -81,7 +81,38 @@ try { if (-not (Test-Path $catalogue)) { Warn "HardwareDriver.json not found at $catalogue - cannot check drivers." } else { - $entries = Get-Content $catalogue -Raw | ConvertFrom-Json + # NOT ConvertFrom-Json. HardwareDriver.json carries both casings of the + # same fields - "fileName" and "FileName", "destinationDir" and + # "DestinationDir". Windows PowerShell 5.1 (which is what WinPE runs) + # treats object keys case-insensitively and throws: + # "cannot convert the JSON string because a dictionary that was + # converted from the string contains the duplicated keys 'FileName' + # and 'FileName'" + # -AsHashtable would handle it but that is PowerShell 6+. So pull the + # four fields we need out of each entry by text instead. Prefers the + # lowercase key, falls back to the capitalised one. + $raw = Get-Content $catalogue -Raw + $entries = @() + foreach ($chunk in ([regex]::Split($raw, '\}\s*,\s*\{'))) { + $get = { + param($names) + foreach ($n in $names) { + $m = [regex]::Match($chunk, '"' + $n + '"\s*:\s*"((?:[^"\\]|\\.)*)"') + if ($m.Success) { return $m.Groups[1].Value -replace '\\\\', '\' } + } + return '' + } + $e = [pscustomobject]@{ + modelswminame = (& $get @('modelswminame','models')) + family = (& $get @('family')) + fileName = (& $get @('fileName','FileName')) + destinationDir = (& $get @('destinationDir','DestinationDir')) + } + if ($e.modelswminame) { $entries += $e } + } + if (-not $entries.Count) { + Warn "Could not extract any entries from $catalogue - driver check skipped." + } $family = '' if ($model.ToUpper().Contains('LATITUDE')) { $family = 'Latitude' } if ($model.ToUpper().Contains('OPTIPLEX')) { $family = 'Optiplex' }