# kb_compare_lite_v1.2.ps1 (regenerated, read-only) # But : Comparer le pointeur KB du BootPack (section [BUG_KB_JSON_POINTER]) avec l'état réel du fichier JSON. # Règles : PS 5.1 strict, ASCII-safe, UTF-8 BOM (ce script). Lecture seule. Aucune écriture. # Params : -Root "<..._registry>" et/ou -BootPackPath "<...bootpack\bootpack.txt|bootpack_paste.txt>" # Sortie : # [KB POINTER] # Expect : SHA= Entries= Size= # Actual : SHA= Entries= Size= [CmdletBinding()] param( [string]$Root = "\\DS-918\\chatgpt\\ChatGPT-Gouvernance-Projets\\_registry", [string]$BootPackPath = "" ) # ---------------- Utils (PS 5.1) ---------------- function Read-Text([string]$Path){ if(-not (Test-Path -LiteralPath $Path)){ return "" } $txt = Get-Content -LiteralPath $Path -Raw -Encoding UTF8 # strip BOM internes $txt = $txt -replace "`uFEFF","" return $txt } function Parse-PointerSection([string]$bootText){ $res = @{ Path=""; SHA=""; Entries=""; Size="" } if([string]::IsNullOrWhiteSpace($bootText)){ return $res } $lines = $bootText -split "(`r`n|`n|`r)" $in = $false foreach($l in $lines){ $t = $l.Trim() if($t -match "^\[BUG_KB_JSON_POINTER\]\s*$"){ $in = $true; continue } if($t -match "^\[.+\]\s*$"){ if($in){ break } } if($in){ if($t.Length -eq 0){ continue } $eq = $t.IndexOf("=") if($eq -gt 0){ $k = $t.Substring(0,$eq).Trim() $v = $t.Substring($eq+1).Trim().Trim('"') if($k -ieq "Path"){ $res.Path = $v } elseif($k -ieq "SHA256"){ $res.SHA = $v } elseif($k -ieq "Entries"){ $res.Entries = $v } elseif($k -ieq "Size"){ $res.Size = $v } } } } return $res } function Read-JsonSansFooter([string]$Path){ if(-not (Test-Path -LiteralPath $Path)){ return "" } $raw = Get-Content -LiteralPath $Path -Raw -Encoding UTF8 $raw = $raw -replace "`uFEFF","" $lastBrace = $raw.LastIndexOf('}') if($lastBrace -gt 0){ $raw = $raw.Substring(0,$lastBrace+1) } return $raw } function Parse-JsonSafe([string]$txt){ if([string]::IsNullOrWhiteSpace($txt)){ return $null } try{ return $txt | ConvertFrom-Json -ErrorAction Stop }catch{ return $null } } function Sha256File([string]$Path){ if(-not (Test-Path -LiteralPath $Path)){ return "MISSING" } try{ return (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToUpperInvariant() }catch{ return "ERROR" } } # ---------------- Entrée bootpack ---------------- if([string]::IsNullOrWhiteSpace($BootPackPath)){ $BootPackPath = Join-Path $Root "bootpack\\bootpack_paste.txt" } $bootText = Read-Text $BootPackPath $ptr = Parse-PointerSection $bootText Write-Host ("[KB POINTER] {0}" -f $BootPackPath) # ---------------- Valeurs attendues ---------------- $expSha = $ptr.SHA $expEntries = $ptr.Entries $expSize = $ptr.Size # ---------------- Valeurs réelles ------------------ $kbPath = $ptr.Path $actSha = Sha256File $kbPath $actSize = "MISSING" $actEntries = -1 if(Test-Path -LiteralPath $kbPath){ try{ $actSize = (Get-Item -LiteralPath $kbPath).Length }catch{ $actSize = "ERROR" } $jsonTxt = Read-JsonSansFooter $kbPath $obj = Parse-JsonSafe $jsonTxt if($null -ne $obj){ if($obj.PSObject.Properties.Match("entries").Count -gt 0 -and $null -ne $obj.entries){ try{ $actEntries = @($obj.entries).Count }catch{ $actEntries = -1 } }else{ $actEntries = -1 } }else{ $actEntries = -1 } } Write-Host ("Expect : SHA={0} Entries={1} Size={2}" -f $expSha,$expEntries,$expSize) Write-Host ("Actual : SHA={0} Entries={1} Size={2}" -f $actSha,$actEntries,$actSize)