Files
Infra-LAB/projects/MDT/Scripts/SupportHDF/ps1/install_choco_bureautique.ps1
T

264 lines
5.2 KiB
PowerShell

<#
Script PowerShell - Installation Chocolatey avec cache local
Optimisé pour MDT USB / Windows 11 25H2
Utilise un dossier local 'softwares' pour accélérer l'installation
Fallback automatique vers Internet si package absent
Gestion timeout + retry + logs
#>
#region CONFIGURATION
$LogFile = "C:\Windows\Temp\choco_install.log"
$GlobalTimeoutMinutes = 180
$PackageTimeoutSeconds = 2700
$RetryCount = 2
# Dossier local contenant les packages .nupkg
$LocalRepo = Join-Path $PSScriptRoot "softwares"
$Packages = @(
"chocolatey-core.extension",
"chocolatey-compatibility.extension",
"chocolatey-windowsupdate.extension",
"vcredist140",
"7zip.install",
"googlechrome",
"firefoxesr",
"microsoft-edge",
"notepadplusplus.install",
"vlc.install",
#"foxitreader",
#"libreoffice-fresh",
"audacity",
#"algobox",
"python3",
"xmind"
)
#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
try {
Add-Content $LogFile $Entry
}
catch {}
}
#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"
)
)
# Rafraîchir les variables d'environnement sans dépendre de Chocolatey
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
}
else {
Write-Log "Chocolatey deja installe"
}
choco feature enable -n allowGlobalConfirmation
#endregion
#region LOCAL REPO CHECK
if (!(Test-Path $LocalRepo)) {
Write-Log "Creation depot local : $LocalRepo"
New-Item -ItemType Directory -Path $LocalRepo -Force
}
Write-Log "Depot local : $LocalRepo"
#endregion
#region DOWNLOAD MISSING PACKAGES
Write-Log "Telechargement packages manquants vers depot local"
foreach ($Package in $Packages) {
Test-GlobalTimeout
$PackageFile = Get-ChildItem $LocalRepo -Filter "$Package*.nupkg" -ErrorAction SilentlyContinue
if (!$PackageFile) {
Write-Log "Telechargement $Package"
$DownloadArgs = "download $Package --source chocolatey --output-directory `"$LocalRepo`" -y --no-progress"
$Process = Start-Process choco -ArgumentList $DownloadArgs -PassThru -WindowStyle Hidden
if ($Process.WaitForExit(300000)) { # 5 minutes timeout
if ($Process.ExitCode -eq 0) {
Write-Log "Telechargement succes $Package"
} else {
Write-Log "Erreur telechargement $Package code $($Process.ExitCode)"
}
} else {
Write-Log "Timeout telechargement $Package"
try { $Process.Kill() } catch {}
}
} else {
Write-Log "Package $Package deja present localement"
}
}
#endregion
#region INSTALL FUNCTION WITH TIMEOUT + LOCAL SOURCE
function Install-PackageWithTimeout {
param (
[string]$PackageName
)
for ($i = 1; $i -le $RetryCount; $i++) {
Test-GlobalTimeout
Write-Log "Installation $PackageName tentative $i"
$Arguments = "install $PackageName -y --no-progress"
if (Test-Path $LocalRepo) {
$PackageFile = Get-ChildItem $LocalRepo -Filter "$PackageName*.nupkg" -ErrorAction SilentlyContinue
if ($PackageFile) {
Write-Log "Installation depuis depot local"
$Arguments += " --source `"$LocalRepo`""
}
else {
Write-Log "Package absent localement - utilisation Internet"
}
}
$Process = Start-Process `
choco `
-ArgumentList $Arguments `
-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