?param( [Parameter(Mandatory=$true)] [string]$Root ) function Sha([string]$p){ try { (Get-FileHash -LiteralPath $p -Algorithm SHA256).Hash.ToLower() } catch { "" } } function Read-HeadJson([string]$p){ if(-not (Test-Path -LiteralPath $p)){ return $null } try{ $raw = Get-Content -LiteralPath $p -Raw ($raw -split "(?m)^--- DOC-VERSION-FOOTER",2)[0] | ConvertFrom-Json } catch { return $null } } function NowIso { (Get-Date).ToString("s") } $kbPath = Join-Path (Join-Path $Root "bug_kb") "BUG_KB.json.txt" $bpDir = Join-Path $Root "bootpack" $pastePath = Join-Path $bpDir "bootpack_paste.txt" $bootPath = Join-Path $bpDir "bootpack.txt" $kbSize = if(Test-Path $kbPath){ (Get-Item $kbPath).Length } else { 0 } $psSize = if(Test-Path $pastePath){ (Get-Item $pastePath).Length } else { 0 } $btSize = if(Test-Path $bootPath){ (Get-Item $bootPath).Length } else { 0 } $kbSha = if($kbSize -gt 0){ Sha $kbPath } else { "" } $psSha = if($psSize -gt 0){ Sha $pastePath } else { "" } $btSha = if($btSize -gt 0){ Sha $bootPath } else { "" } $kbObj = Read-HeadJson $kbPath $psObj = $null; try { if(Test-Path $pastePath){ (Get-Content -LiteralPath $pastePath -Raw) | ConvertFrom-Json | Out-Null; $psObj = (Get-Content -LiteralPath $pastePath -Raw | ConvertFrom-Json) } } catch { $psObj=$null } $kbEnt = if($kbObj -ne $null -and $kbObj.entries){ $kbObj.entries.Count } else { -1 } $psEnt = if($psObj -ne $null -and $psObj.entries){ $psObj.entries.Count } else { -1 } $kbNonA = if($kbSize -gt 0){ ([regex]::Matches((Get-Content -LiteralPath $kbPath -Raw),'[^\x09\x0A\x0D\x20-\x7E]')).Count } else { 0 } $psNonA = if($psSize -gt 0){ ([regex]::Matches((Get-Content -LiteralPath $pastePath -Raw),'[^\x09\x0A\x0D\x20-\x7E]')).Count } else { 0 } Write-Host ("== SELF-CHECK KB/BOOTPACK v1.0 :: {0} ==" -f (NowIso)) Write-Host ("Root: {0}" -f $Root) Write-Host ("KB : size={0} sha={1} entries={2} nonAscii={3}" -f $kbSize,$kbSha,$kbEnt,$kbNonA) Write-Host ("PASTE : size={0} sha={1} entries={2} nonAscii={3}" -f $psSize,$psSha,$psEnt,$psNonA) Write-Host ("BOOT : size={0} sha={1}" -f $btSize,$btSha) $pass = $true if(-not (Test-Path $kbPath)) { Write-Host " - KB missing" ; $pass=$false } if(-not (Test-Path $pastePath)) { Write-Host " - PASTE missing" ; $pass=$false } if(-not (Test-Path $bootPath)) { Write-Host " - BOOTPACK missing" ; $pass=$false } if($kbObj -eq $null) { Write-Host " - KB parse FAIL" ; $pass=$false } if($psObj -eq $null) { Write-Host " - PASTE parse FAIL" ; $pass=$false } if($pass){ Write-Host "Result: PASS" } else { Write-Host "Result: FAIL" }