?# kb_pointer_refresh_v1.0.1.ps1 - Recompute & rewrite KB pointer block (line-based, PS 5.1-safe) param( [Parameter(Mandatory=$true)][string]$RegistryRoot, [switch]$Write ) try{ [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding($false) }catch{} function Read-Text([string]$p){ if(-not (Test-Path -LiteralPath $p)){ return '' } try{ return Get-Content -LiteralPath $p -Raw -Encoding UTF8 }catch{ return (Get-Content -LiteralPath $p -Raw) } } function Split-Lines([string]$t){ if($null -eq $t){ return @() } return ($t -split "(`r?`n)") } $bp = Join-Path $RegistryRoot 'bootpack\bootpack.txt' $pp = Join-Path $RegistryRoot 'bootpack\bootpack_paste.txt' if(-not (Test-Path -LiteralPath $bp)){ Write-Host "[ERROR] bootpack.txt introuvable: $bp"; exit 3 } if(-not (Test-Path -LiteralPath $pp)){ Write-Host "[ERROR] bootpack_paste.txt introuvable: $pp"; exit 3 } $rawBP = Read-Text $bp $lines = Split-Lines $rawBP if($lines.Length -eq 0){ $lines = @('') } # ensure array # compute paste metrics $rawPaste = Read-Text $pp try{ $sha = (Get-FileHash -Algorithm SHA256 -LiteralPath $pp).Hash.ToUpperInvariant() }catch{ $sha='' } try{ $size = (Get-Item -LiteralPath $pp).Length }catch{ $size=-1 } # count entries (simple heuristic, avoid regex flags) $ents = -1 try{ $o = $rawPaste | ConvertFrom-Json if($o -and $o.PSObject.Properties.Name -contains 'entries'){ $ents = @($o.entries).Count } }catch{ } if($ents -lt 0){ try{ $count=0 $idx = 0 while($true){ $i = $rawPaste.IndexOf('"id"',$idx) if($i -lt 0){ break } $count++; $idx = $i + 4 } $ents = $count }catch{ $ents = -1 } } # build new pointer block (lines) $blk = @() $blk += '[BUG_KB_JSON_POINTER]' $blk += ('Path : {0}' -f $pp) $blk += ('SHA256 : {0}' -f $sha) $blk += ('Entries : {0}' -f $ents) $blk += ('SizeBytes : {0}' -f $size) $blk += '' # locate existing block (line-based) $start = -1; $end = -1 for($i=0; $i -lt $lines.Length; $i++){ if($lines[$i].Trim() -ceq '[BUG_KB_JSON_POINTER]'){ $start=$i; break } } if($start -ge 0){ $end = $lines.Length-1 for($j=$start+1; $j -lt $lines.Length; $j++){ $t = $lines[$j] if($t -match '^\['){ $end = $j-1; break } } } # rebuild lines without old block $new = @() if($start -ge 0){ if($start -gt 0){ $new += $lines[0..($start-1)] } $new += $blk if($end+1 -le $lines.Length-1){ $new += $lines[($end+1)..($lines.Length-1)] } }else{ # insert at top by convention $new += $blk $new += $lines } # ensure/update single-line summary "KB Pointer :" $foundLine = $false for($k=0; $k -lt $new.Length; $k++){ if($new[$k] -match '^\s*KB\W*Pointer\s*:'){ $new[$k] = ('KB Pointer : {0} (SHA={1}, Entries={2}, Size={3})' -f $pp,$sha,$ents,$size) $foundLine = $true break } } if(-not $foundLine){ $new += ('KB Pointer : {0} (SHA={1}, Entries={2}, Size={3})' -f $pp,$sha,$ents,$size) $new += '' } Write-Host ("[POINTER] Path={0}" -f $pp) Write-Host ("Values : SHA={0} Entries={1} Size={2}" -f $sha,$ents,$size) # write or preview $txt = ($new -join "`r`n") if($Write){ $bak = $bp + "." + (Get-Date).ToString('yyyyMMdd_HHmmss') + ".bak" try{ Copy-Item -LiteralPath $bp -Destination $bak -Force }catch{} $enc = New-Object System.Text.UTF8Encoding($false) [IO.File]::WriteAllText($bp,$txt,$enc) Write-Host ("STATUS=WRITE-OK -> {0}" -f $bp) exit 0 }else{ Write-Host "PREVIEW (no write)." exit 0 }