diff --git a/cleanup-windows.bat b/cleanup-windows.bat new file mode 100644 index 0000000..f83aea4 --- /dev/null +++ b/cleanup-windows.bat @@ -0,0 +1,16 @@ +@echo off +REM ════════════════════════════════════════════════════════════════ +REM ARIA — Cleanup-Wrapper fuer Windows +REM ════════════════════════════════════════════════════════════════ +REM Ruft cleanup-windows.ps1 mit ExecutionPolicy Bypass auf. +REM Funktioniert auch wenn Windows .ps1 direkt nicht startet. +REM +REM Nutzung: +REM cleanup-windows.bat stefan +REM cleanup-windows.bat stefan -SkipPrune +REM +REM Doppelklick funktioniert NICHT (braucht Username als Param). +REM Per Konsole aufrufen. +REM ════════════════════════════════════════════════════════════════ + +powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0cleanup-windows.ps1" %* diff --git a/cleanup-windows.ps1 b/cleanup-windows.ps1 index 7f339a0..281c16c 100644 --- a/cleanup-windows.ps1 +++ b/cleanup-windows.ps1 @@ -34,14 +34,33 @@ param( [switch]$PruneOnly ) -# Admin-Check +# Defensive: Process-Scope ExecutionPolicy auf Bypass — verhindert dass +# Untersaetze (z.B. Module) blockiert werden. Harmless wenn Parent schon +# Bypass aufgerufen hat. +try { Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force | Out-Null } catch {} + +# Admin-Check + Self-Elevation +# Wenn nicht als Admin gestartet → einmal neu starten als Admin, mit +# ExecutionPolicy Bypass + den Original-Argumenten. User muss nur einmal +# UAC-Prompt bestaetigen. $isAdmin = ([Security.Principal.WindowsPrincipal] ` [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator) if (-not $isAdmin) { - Write-Host "❌ Dieses Script muss als Administrator laufen." -ForegroundColor Red - Write-Host " Rechtsklick auf PowerShell → 'Als Administrator ausfuehren'" -ForegroundColor Yellow - exit 1 + Write-Host "→ Starte neu als Administrator (mit ExecutionPolicy Bypass)..." -ForegroundColor Yellow + $myPath = $MyInvocation.MyCommand.Path + $forwardArgs = @("-NoProfile", "-ExecutionPolicy", "Bypass", "-File", "`"$myPath`"") + if ($User) { $forwardArgs += @("-User", $User) } + if ($SkipPrune) { $forwardArgs += "-SkipPrune" } + if ($PruneOnly) { $forwardArgs += "-PruneOnly" } + try { + Start-Process powershell.exe -Verb RunAs -ArgumentList $forwardArgs + } catch { + Write-Host "❌ UAC-Elevation abgebrochen oder fehlgeschlagen." -ForegroundColor Red + Write-Host " Rechtsklick auf PowerShell → 'Als Administrator ausfuehren'" -ForegroundColor Yellow + exit 1 + } + exit 0 } $basePath = "C:\Users\$User\AppData\Local"