This commit is contained in:
Joeri 2026-02-07 12:19:16 +01:00
parent 838b07d998
commit 5038877cd7

View file

@ -500,27 +500,107 @@ try {
Write-Log "Warning: Could not stop Edge processes: $($_.Exception.Message)" -Level "WARNING" Write-Log "Warning: Could not stop Edge processes: $($_.Exception.Message)" -Level "WARNING"
} }
# Check for installed BleachBit # Download BleachBit Portable
Write-Log "Checking for BleachBit installation..." -Level "INFO" Write-Log "Downloading BleachBit Portable..." -Level "INFO"
$BleachBitExe = $null $BleachBitZip = "C:\Quest\BleachBitPortable.zip"
$BleachBitUrl = "https://www.bleachbit.org/download/file/t?file=BleachBit-5.0.2-portable.zip"
$MinFileSizeBytes = 10000000 # Minimum 10MB
# Check common installation paths try {
$PossiblePaths = @( # Remove old zip file if exists
"C:\Program Files\BleachBit\bleachbit_console.exe", if (Test-Path $BleachBitZip) {
"C:\Program Files (x86)\BleachBit\bleachbit_console.exe", Remove-Item $BleachBitZip -Force -ErrorAction Stop
"C:\Quest\BleachBit-Portable\bleachbit_console.exe" }
)
foreach ($Path in $PossiblePaths) { # Download with retry logic
if (Test-Path $Path) { $DownloadSuccess = $false
$BleachBitExe = $Path $RetryCount = 0
Write-Log "Found BleachBit at: $BleachBitExe" -Level "INFO" $MaxRetries = 3
break
while (-not $DownloadSuccess -and $RetryCount -lt $MaxRetries) {
$RetryCount++
Write-Log "Download attempt $RetryCount of $MaxRetries..." -Level "INFO"
try {
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile($BleachBitUrl, $BleachBitZip)
$DownloadSuccess = $true
Write-Log "Download completed." -Level "INFO"
} catch {
Write-Log "Download attempt failed: $($_.Exception.Message)" -Level "WARNING"
Start-Sleep -Seconds 5
} }
} }
# Run BleachBit cleanup if found if (-not $DownloadSuccess) {
if ($BleachBitExe) { throw "Failed to download after $MaxRetries attempts"
}
# Verify file size
$FileSize = (Get-Item $BleachBitZip).Length
Write-Log "Downloaded file size: $($FileSize / 1MB) MB" -Level "INFO"
if ($FileSize -lt $MinFileSizeBytes) {
throw "Downloaded file is too small ($($FileSize / 1MB) MB). Expected at least $($MinFileSizeBytes / 1MB) MB."
}
$ScriptResult.bleachbit_cleanup.downloaded = $true
Write-Log "BleachBit Portable downloaded successfully." -Level "INFO"
} catch {
$ErrorMsg = "Failed to download BleachBit Portable: $($_.Exception.Message)"
$ScriptResult.bleachbit_cleanup.errors += $ErrorMsg
Write-Log $ErrorMsg -Level "ERROR"
}
# Extract BleachBit Portable
if ($ScriptResult.bleachbit_cleanup.downloaded) {
Write-Log "Extracting BleachBit Portable..." -Level "INFO"
$BleachBitDest = "C:\Quest"
try {
# Remove old extraction folder if exists
$OldBleachBitPath = Join-Path $BleachBitDest "BleachBit-Portable"
if (Test-Path $OldBleachBitPath) {
Remove-Item $OldBleachBitPath -Recurse -Force -ErrorAction Stop
}
# Extract using Expand-Archive
Expand-Archive -Path $BleachBitZip -DestinationPath $BleachBitDest -Force -ErrorAction Stop
$ScriptResult.bleachbit_cleanup.extracted = $true
Write-Log "BleachBit Portable extracted successfully." -Level "INFO"
# Remove zip file
Remove-Item $BleachBitZip -Force -ErrorAction SilentlyContinue
Write-Log "Removed temporary zip file." -Level "INFO"
} catch {
$ErrorMsg = "Failed to extract BleachBit Portable: $($_.Exception.Message)"
$ScriptResult.bleachbit_cleanup.errors += $ErrorMsg
Write-Log $ErrorMsg -Level "ERROR"
# Try alternative extraction using shell
try {
Write-Log "Trying alternative extraction method..." -Level "WARNING"
$shell = New-Object -ComObject Shell.Application
$zip = $shell.NameSpace($BleachBitZip)
$dest = $shell.NameSpace($BleachBitDest)
$dest.CopyHere($zip.items(), 16)
Start-Sleep -Seconds 5
$ScriptResult.bleachbit_cleanup.extracted = $true
Write-Log "BleachBit Portable extracted using shell method." -Level "INFO"
# Remove zip file
Remove-Item $BleachBitZip -Force -ErrorAction SilentlyContinue
} catch {
Write-Log "Alternative extraction also failed: $($_.Exception.Message)" -Level "ERROR"
}
}
}
# Run BleachBit cleanup
$BleachBitExe = "C:\Quest\BleachBit-Portable\bleachbit_console.exe"
if (Test-Path $BleachBitExe) {
Write-Log "Running BleachBit cleanup with $($BleachBitCleaners.Count) cleaners..." -Level "INFO" Write-Log "Running BleachBit cleanup with $($BleachBitCleaners.Count) cleaners..." -Level "INFO"
try { try {
@ -531,8 +611,16 @@ if ($BleachBitExe) {
$BleachBitProcess = Start-Process -FilePath $BleachBitExe -ArgumentList "--clean $CleanerArgs" -NoNewWindow -Wait -PassThru -ErrorAction Stop $BleachBitProcess = Start-Process -FilePath $BleachBitExe -ArgumentList "--clean $CleanerArgs" -NoNewWindow -Wait -PassThru -ErrorAction Stop
$ScriptResult.bleachbit_cleanup.cleaners_run = $BleachBitCleaners.Count $ScriptResult.bleachbit_cleanup.cleaners_run = $BleachBitCleaners.Count
$ScriptResult.bleachbit_cleanup.extracted = $true
Write-Log "BleachBit cleanup completed. Exit code: $($BleachBitProcess.ExitCode)" -Level "INFO" Write-Log "BleachBit cleanup completed. Exit code: $($BleachBitProcess.ExitCode)" -Level "INFO"
# Clean up BleachBit files after use
try {
Write-Log "Cleaning up BleachBit Portable files..." -Level "INFO"
Remove-Item "C:\Quest\BleachBit-Portable" -Recurse -Force -ErrorAction SilentlyContinue
Write-Log "BleachBit Portable files removed." -Level "INFO"
} catch {
Write-Log "Warning: Could not remove BleachBit files: $($_.Exception.Message)" -Level "WARNING"
}
} catch { } catch {
$ErrorMsg = "Failed to run BleachBit cleanup: $($_.Exception.Message)" $ErrorMsg = "Failed to run BleachBit cleanup: $($_.Exception.Message)"
$ScriptResult.bleachbit_cleanup.errors += $ErrorMsg $ScriptResult.bleachbit_cleanup.errors += $ErrorMsg
@ -540,8 +628,7 @@ if ($BleachBitExe) {
Write-Log $ErrorMsg -Level "ERROR" Write-Log $ErrorMsg -Level "ERROR"
} }
} else { } else {
Write-Log "BleachBit not found. Skipping cleanup." -Level "WARNING" Write-Log "BleachBit executable not found at: $BleachBitExe" -Level "ERROR"
Write-Log "Install BleachBit to enable automatic cleanup." -Level "INFO"
} }
Write-Log "BleachBit cleanup section completed." -Level "INFO" Write-Log "BleachBit cleanup section completed." -Level "INFO"