??$ErrorActionPreference='Stop' $root="\\DS-918\chatgpt\ChatGPT-Gouvernance-Projets\_registry" $scripts=Join-Path $root "scripts" $inbox=Join-Path $root "updates\\inbox\\kb" if(!(Test-Path $inbox)){ New-Item -ItemType Directory -Force -Path $inbox|Out-Null } # d?pendances $psQuick = Join-Path $scripts "kb_quicklog_and_sync_v1.1.ps1" $psAuto = Join-Path $scripts "kb_autodraft_from_err_v1.0.ps1" function __kb-Capture { try{ if($global:__kbGuardBusy){ return } $h = Get-History -Count 1 -ErrorAction SilentlyContinue; if(-not $h){ return } if($h.Id -eq $global:__kbLastHistoryId){ return } $failed = ($LASTEXITCODE -ne $null -and $LASTEXITCODE -ne 0) -or ($error.Count -gt ($global:__kbPrevErrorCount|?{$_}|Measure-Object).Count) $global:__kbPrevErrorCount = $error.Count $global:__kbLastHistoryId = $h.Id if(-not $failed){ return } $cmd = $h.CommandLine if($cmd -match 'kb_quicklog_and_sync|kb_autodraft_from_err'){ return } $global:__kbGuardBusy=$true $ts = Get-Date -Format 'yyyyMMdd_HHmmss' $errFile = Join-Path $inbox ("INTERACTIVE_{0}.err.txt" -f $ts) $codeFile= Join-Path $inbox ("INTERACTIVE_{0}.code.ps1" -f $ts) [IO.File]::WriteAllText($codeFile,$cmd,(New-Object Text.UTF8Encoding($true))) $errMsg = ($error[0] | Out-String) [IO.File]::WriteAllText($errFile,$errMsg,(New-Object Text.UTF8Encoding($true))) $ps = "$env:SystemRoot\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" if(Test-Path $psAuto){ & $ps -NoProfile -ExecutionPolicy Bypass -File $psAuto -ErrFile $errFile -ScriptPath $codeFile -Tags oneshot,interactive | Out-Null } $last = Get-ChildItem -LiteralPath $inbox -Filter 'AUTO_INTERACTIVE_*.txt' -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending | Select-Object -First 1 if(-not $last){ $last = Get-ChildItem -LiteralPath $inbox -Filter 'AUTO_*.txt' -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending | Select-Object -First 1 } if($last -and (Test-Path $psQuick)){ & $ps -NoProfile -ExecutionPolicy Bypass -File $psQuick -FromFile $last.FullName -Execute | Out-Null } } catch {} finally { $global:__kbGuardBusy=$false } } # Alias utilitaire (facultatif) pour historiser un succ?s volontaire function kb { param([ScriptBlock]$ScriptBlock,[string]$Title="",[switch]$AutoSuccessNote) $had=$false; try{ & $ScriptBlock } catch { $had=$true; throw } finally { if(-not $had -and $AutoSuccessNote -and $Title){ $today=Get-Date -Format 'yyyy-MM-dd'; $form=Join-Path $inbox ("SUCCESS_{0}.txt" -f (Get-Date -Format 'yyyyMMdd_HHmmss')) $lines=@("Title: {0}" -f $Title,"Blocking: false","RootCause: ","Workaround: ","Fix: Validated oneshot", "Tags: oneshot,success","SeenInThreads: {0}" -f $today) [IO.File]::WriteAllText($form,($lines -join "`r`n"),(New-Object Text.UTF8Encoding($true))) $ps="$env:SystemRoot\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" if(Test-Path $psQuick){ & $ps -NoProfile -ExecutionPolicy Bypass -File $psQuick -FromFile $form -Execute | Out-Null } } } } # Hook prompt non intrusif (cha?ne les prompts existants) if(-not $global:__kbPrevErrorCount){ $global:__kbPrevErrorCount = $error.Count } if(-not $global:__kbLastHistoryId){ $global:__kbLastHistoryId = 0 } $__origPrompt = $function:prompt function prompt { __kb-Capture if($__origPrompt){ & $__origPrompt } else { "PS " + (Get-Location) + "> " } } Write-Host "[GOV] Archiviste interactif arm? (z?ro profil global touch?)" -ForegroundColor Green