#Requires -RunAsAdministrator <# .SYNOPSIS Installe ou répare WSL2 de manière fiable. .DESCRIPTION Ce script : 1. Vérifie si WSL2 est déjà prêt. 2. Active les fonctionnalités Windows nécessaires. 3. Définit WSL2 comme version par défaut. 4. Met à jour le noyau WSL2. 5. Installe WSL sans distribution si possible. Un redémarrage peut être nécessaire après l’activation des fonctionnalités. #> $ErrorActionPreference = "Stop" function Test-Wsl2Ready { try { $output = & wsl.exe --status 2>&1 $exitCode = $LASTEXITCODE Write-Host "[Test] wsl --status exit code: $exitCode" -ForegroundColor Cyan if ($output) { Write-Host "[Test] wsl --status output:" -ForegroundColor Cyan $output | ForEach-Object { Write-Host " $_" -ForegroundColor Gray } } if ($exitCode -eq 0 -or ($output -match "Version par défaut\s*:\s*2") -or ($output -match "Default Version\s*:\s*2")) { return $true } } catch { Write-Host "[Test] wsl --status a échoué : $_" -ForegroundColor Yellow } return $false } function Enable-WindowsFeatureIfNeeded { param([string]$FeatureName) Write-Host "[Feature] Activation de $FeatureName..." -ForegroundColor Cyan $result = & dism.exe /online /enable-feature /featurename:$FeatureName /all /norestart 2>&1 $exitCode = $LASTEXITCODE $result | ForEach-Object { Write-Host " $_" -ForegroundColor Gray } if ($exitCode -eq 0) { Write-Host "[Feature] $FeatureName activé (pas de redémarrage nécessaire)." -ForegroundColor Green return $false } elseif ($exitCode -eq 3010) { Write-Host "[Feature] $FeatureName activé, mais un redémarrage est nécessaire (code 3010)." -ForegroundColor Yellow return $true } else { throw "Échec de l'activation de $FeatureName (code $exitCode)." } } function Install-Wsl2 { Write-Host "[WSL] Tentative d'installation sans distribution..." -ForegroundColor Cyan try { & wsl.exe --install --no-distribution 2>&1 | ForEach-Object { Write-Host " $_" -ForegroundColor Gray } if ($LASTEXITCODE -ne 0) { throw "wsl --install --no-distribution a retourné le code $LASTEXITCODE" } Write-Host "[WSL] Installation sans distribution réussie." -ForegroundColor Green return } catch { Write-Host "[WSL] Option --no-distribution non supportée ou échec : $_" -ForegroundColor Yellow } Write-Host "[WSL] Fallback : installation classique de WSL..." -ForegroundColor Cyan & wsl.exe --install 2>&1 | ForEach-Object { Write-Host " $_" -ForegroundColor Gray } if ($LASTEXITCODE -ne 0) { throw "wsl --install a retourné le code $LASTEXITCODE" } Write-Host "[WSL] Installation classique réussie." -ForegroundColor Green } # === Début du script === Write-Host "=== Installation / réparation WSL2 ===" -ForegroundColor Green if (Test-Wsl2Ready) { Write-Host "WSL2 est déjà prêt. Rien à faire." -ForegroundColor Green exit 0 } Write-Host "WSL2 n'est pas détecté. Lancement de l'installation..." -ForegroundColor Yellow $rebootNeeded = $false $rebootNeeded = (Enable-WindowsFeatureIfNeeded -FeatureName "Microsoft-Windows-Subsystem-Linux") -or $rebootNeeded $rebootNeeded = (Enable-WindowsFeatureIfNeeded -FeatureName "VirtualMachinePlatform") -or $rebootNeeded if ($rebootNeeded) { Write-Host "`nUn redémarrage est nécessaire pour activer les fonctionnalités Windows." -ForegroundColor Yellow Write-Host "Après le redémarrage, relance ce script pour terminer l'installation de WSL2." -ForegroundColor Yellow $response = Read-Host "Redémarrer maintenant ? (O/N)" if ($response -eq "O" -or $response -eq "o") { Restart-Computer -Force } exit 3010 } Write-Host "[WSL] Définition de WSL2 comme version par défaut..." -ForegroundColor Cyan & wsl.exe --set-default-version 2 2>&1 | ForEach-Object { Write-Host " $_" -ForegroundColor Gray } if ($LASTEXITCODE -ne 0) { throw "wsl --set-default-version 2 a échoué (code $LASTEXITCODE)." } Write-Host "[WSL] Mise à jour du noyau WSL2..." -ForegroundColor Cyan & wsl.exe --update 2>&1 | ForEach-Object { Write-Host " $_" -ForegroundColor Gray } # 3010 = succès mais redémarrage possible if ($LASTEXITCODE -ne 0 -and $LASTEXITCODE -ne 3010) { throw "wsl --update a échoué (code $LASTEXITCODE)." } Install-Wsl2 if (Test-Wsl2Ready) { Write-Host "`nWSL2 est maintenant prêt." -ForegroundColor Green exit 0 } else { Write-Host "`nWSL2 ne semble toujours pas prêt. Essayez de redémarrer et de relancer le script." -ForegroundColor Red exit 1 }