# Script de synchronisation vers C:\MDT # Met a jour uniquement les fichiers modifies ou manquants $ErrorActionPreference = "Continue" param( [string]$SourceBase = 'C:\Dev\Infra-LAB\projects\MDT', [string]$DestinationBase = 'C:\MDT', [switch]$DryRun = $false ) # Couleurs pour l'affichage $colors = @{ Success = "Green" Error = "Red" Warning = "Yellow" Info = "Cyan" Update = "Magenta" } function Write-ColorOutput { param( [string]$Message, [string]$Color = "White" ) Write-Host $Message -ForegroundColor $Color } function Sync-Folder { param( [string]$SourceFolder, [string]$DestFolder ) try { # Extraire le nom du dossier a partir du chemin source $FolderName = Split-Path -Leaf $SourceFolder Write-ColorOutput "`n=== Synchronisation du dossier: $FolderName ===" -Color $colors.Info # Verifier si le dossier source existe if (-not (Test-Path $SourceFolder)) { Write-ColorOutput "ERREUR: Le dossier source n existe pas: $SourceFolder" -Color $colors.Error return $false } # Creer le dossier destination s il n existe pas if (-not (Test-Path $DestFolder)) { Write-ColorOutput "Creation du dossier de destination: $DestFolder" -Color $colors.Warning if (-not $DryRun) { New-Item -ItemType Directory -Path $DestFolder -Force | Out-Null } } $filesUpdated = 0 $filesAdded = 0 $filesSkipped = 0 # Recuperer tous les fichiers du dossier source (recursivement) $sourceFiles = @(Get-ChildItem -Path $SourceFolder -Recurse -File -ErrorAction Stop) Write-ColorOutput "Fichiers detectes: $($sourceFiles.Count)" -Color $colors.Info foreach ($sourceFile in $sourceFiles) { # Calculer le chemin relatif $relativePath = $sourceFile.FullName.Substring($SourceFolder.Length).TrimStart('\') $destPath = Join-Path $DestFolder $relativePath $destDir = Split-Path $destPath -Parent # Creer le dossier de destination s il n existe pas if (-not (Test-Path $destDir)) { Write-ColorOutput " -> Creation du repertoire: $destDir" -Color $colors.Warning if (-not $DryRun) { New-Item -ItemType Directory -Path $destDir -Force | Out-Null } } # Verifier si le fichier destination existe if (-not (Test-Path $destPath)) { # Fichier manquant - copier Write-ColorOutput " [NOUVEAU] $relativePath" -Color $colors.Success $filesAdded++ if (-not $DryRun) { Copy-Item -Path $sourceFile.FullName -Destination $destPath -Force } } else { # Fichier existe - comparer les hashes $sourceHash = (Get-FileHash -Path $sourceFile.FullName -Algorithm MD5).Hash $destHash = (Get-FileHash -Path $destPath -Algorithm MD5).Hash if ($sourceHash -ne $destHash) { # Fichier modifie - copier Write-ColorOutput " [MODIFIE] $relativePath" -Color $colors.Update $filesUpdated++ if (-not $DryRun) { Copy-Item -Path $sourceFile.FullName -Destination $destPath -Force } } else { # Fichier identique - ignorer $filesSkipped++ } } } Write-ColorOutput "`nResume - ${FolderName}:" -Color $colors.Info Write-ColorOutput " [+] Fichiers ajoutes: $filesAdded" -Color $colors.Success Write-ColorOutput " [~] Fichiers mis a jour: $filesUpdated" -Color $colors.Update Write-ColorOutput " [=] Fichiers inchanges: $filesSkipped" -Color $colors.Info return $true } catch { Write-ColorOutput "ERREUR CRITIQUE dans Sync-Folder: $_" -Color $colors.Error Write-ColorOutput "Source: $SourceFolder" -Color $colors.Error Write-ColorOutput "Destination: $DestFolder" -Color $colors.Error return $false } } function Main { Write-ColorOutput "================================================" -Color $colors.Info Write-ColorOutput "Script de Synchronisation vers C:\MDT" -Color $colors.Info Write-ColorOutput "================================================" -Color $colors.Info if ($DryRun) { Write-ColorOutput "`n[!] MODE DRY-RUN ACTIVE - pas de modification reelle" -Color $colors.Warning } Write-ColorOutput "`nSource: $SourceBase" -Color $colors.Info Write-ColorOutput "Destination: $DestinationBase" -Color $colors.Info Write-ColorOutput "`nVerification des chemins:" -Color $colors.Info $controlSource = "$SourceBase\Control" $scriptsSource = "$SourceBase\Scripts" $controlExists = Test-Path $controlSource $scriptsExists = Test-Path $scriptsSource Write-ColorOutput " Controle source: $controlSource (existe: $controlExists)" -Color $colors.Info Write-ColorOutput " Scripts source: $scriptsSource (existe: $scriptsExists)" -Color $colors.Info # Verifier si C:\MDT existe, sinon creer if (-not (Test-Path $DestinationBase)) { Write-ColorOutput "`nLe dossier C:\MDT n existe pas. Creation en cours..." -Color $colors.Warning if (-not $DryRun) { New-Item -ItemType Directory -Path $DestinationBase -Force | Out-Null } } # Synchroniser les deux dossiers $success = $true Write-ColorOutput "`nDemarrage de la synchronisation..." -Color $colors.Info $success = Sync-Folder -SourceFolder "$SourceBase\Control" -DestFolder "$DestinationBase\Control" $success = Sync-Folder -SourceFolder "$SourceBase\Scripts" -DestFolder "$DestinationBase\Scripts" Write-ColorOutput "`n================================================" -Color $colors.Info if ($success) { Write-ColorOutput "[OK] Synchronisation terminee avec succes" -Color $colors.Success } else { Write-ColorOutput "[ERROR] Synchronisation terminee avec erreurs" -Color $colors.Error } Write-ColorOutput "================================================" -Color $colors.Info } # Executer le script principal Main