# deposit_lot1_blocking_fix_v1.0.ps1 # Objet : Déposer un lot de scripts corrigés depuis C:\GovDrop\incoming\lot1 vers \\..._registry\scripts # via SAFE-CREATE : hash -> preview -> O/N -> move atomique -> re-hash ; .bak horodaté. # PS 5.1 strict, UTF-8 BOM. param( [string]$Root="\\DS-918\chatgpt\ChatGPT-Gouvernance-Projets\_registry", [string]$InDir="C:\GovDrop\incoming\lot1", [switch]$Preview, [switch]$Execute ) if($Preview -and $Execute){ Write-Host "[NOK] -Preview et -Execute exclusifs."; exit 1 } if(-not $Preview -and -not $Execute){ $Preview = $true } function NowIso(){ (Get-Date).ToString("yyyyMMdd_HHmmss") } function Ensure-Parent([string]$p){ $parent=[IO.Path]::GetDirectoryName($p) if(-not [string]::IsNullOrWhiteSpace($parent)){ [void][IO.Directory]::CreateDirectory($parent) } } function Sha([string]$p){ if(Test-Path -LiteralPath $p){ (Get-FileHash -LiteralPath $p -Algorithm SHA256).Hash } else { "" } } $dstDir = Join-Path -Path $Root -ChildPath "scripts" if(-not (Test-Path -LiteralPath $InDir)){ Write-Host ("[NOK] InDir introuvable : {0}" -f $InDir); exit 2 } if(-not (Test-Path -LiteralPath $dstDir)){ Write-Host ("[NOK] Dossier de destination introuvable : {0}" -f $dstDir); exit 3 } $files = Get-ChildItem -LiteralPath $InDir -Filter *.ps1 -File -ErrorAction SilentlyContinue | Sort-Object Name Write-Host ("== PREVIEW DEPOT :: {0} fichier(s) ==" -f $files.Count) foreach($f in $files){ $dst = Join-Path -Path $dstDir -ChildPath $f.Name Write-Host ("DEPOT {0} -> {1}" -f $f.FullName, $dst) } if($Preview){ Write-Host "[INFO] Preview : aucune écriture." Write-Host ("Pour exécuter :`n powershell -NoProfile -ExecutionPolicy Bypass -File `"{0}`" -Execute" -f $MyInvocation.MyCommand.Definition) exit 0 } if($Execute){ $ts = NowIso $ok=0; $errors=0 foreach($f in $files){ $dst = Join-Path -Path $dstDir -ChildPath $f.Name $tmp = Join-Path -Path (Join-Path -Path $Root -ChildPath "__tmp_deposit") -ChildPath $f.Name Ensure-Parent $tmp Copy-Item -LiteralPath $f.FullName -Destination $tmp -Force $shaLocal = Sha $f.FullName $shaTmp = Sha $tmp if($shaLocal -ne $shaTmp){ Write-Host ("[NOK] SHA local/tmp diff -> {0}" -f $f.Name); $errors++; continue } if(Test-Path -LiteralPath $dst){ $bak = $dst + "." + $ts + ".bak" Rename-Item -LiteralPath $dst -NewName (Split-Path -Leaf $bak) -Force } Ensure-Parent $dst Move-Item -LiteralPath $tmp -Destination $dst -Force $shaFinal = Sha $dst if($shaFinal -ne $shaLocal){ Write-Host ("[NOK] SHA final diff -> {0}" -f $f.Name); $errors++ } else { $ok++ } } Write-Host ("== RESULTAT == OK={0} Errors={1}" -f $ok, $errors) exit ([int]([bool]($errors -gt 0))) }