<# Installation Chocolatey + logiciels Optimisé MDT USB / Windows 11 25H2 Gestion timeout + retry #> #region CONFIGURATION $LogFile = "C:\Windows\Temp\choco_install.log" $GlobalTimeoutMinutes = 180 $PackageTimeoutSeconds = 2700 $RetryCount = 2 $Packages = @( "chocolatey-core.extension", "chocolatey-compatibility.extension", "chocolatey-windowsupdate.extension", "7zip.install", "googlechrome", "firefoxesr", "microsoft-edge", "notepadplusplus.install", "vlc.install", "foxitreader", "libreoffice-fresh", "vcredist140" ) #endregion #region LOGGING function Write-Log { param ([string]$Message) $Time = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $Entry = "$Time - $Message" Write-Output $Entry Add-Content $LogFile $Entry } #endregion #region GLOBAL TIMER $ScriptStartTime = Get-Date function Test-GlobalTimeout { $Elapsed = (Get-Date) - $ScriptStartTime if ($Elapsed.TotalMinutes -gt $GlobalTimeoutMinutes) { Write-Log "GLOBAL TIMEOUT atteint" exit 1 } } #endregion #region PREREQUIS Write-Log "Verification prerequis" Set-ExecutionPolicy Bypass -Scope Process -Force [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072 #endregion #region INSTALL CHOCOLATEY if (!(Get-Command choco -ErrorAction SilentlyContinue)) { Write-Log "Installation Chocolatey" Invoke-Expression ( (New-Object System.Net.WebClient).DownloadString( "https://community.chocolatey.org/install.ps1" ) ) refreshenv } else { Write-Log "Chocolatey deja installe" } choco feature enable -n allowGlobalConfirmation #endregion #region INSTALL FUNCTION WITH TIMEOUT function Install-PackageWithTimeout { param ( [string]$PackageName ) for ($i = 1; $i -le $RetryCount; $i++) { Test-GlobalTimeout Write-Log "Installation $PackageName tentative $i" $Process = Start-Process ` choco ` -ArgumentList "install $PackageName -y --no-progress" ` -PassThru ` -WindowStyle Hidden if ($Process.WaitForExit($PackageTimeoutSeconds * 1000)) { if ($Process.ExitCode -eq 0) { Write-Log "Succes $PackageName" return } else { Write-Log "Erreur code $($Process.ExitCode)" } } else { Write-Log "Timeout $PackageName" try { $Process.Kill() } catch {} } Start-Sleep 10 } Write-Log "Echec final $PackageName" } #endregion #region INSTALLATION PACKAGES foreach ($Package in $Packages) { Install-PackageWithTimeout $Package } #endregion #region UPGRADE FINAL Write-Log "Upgrade global" choco upgrade all -y --no-progress #endregion Write-Log "Script termine" exit 0