-
This commit is contained in:
parent
fa7ea11799
commit
54e88a7781
1 changed files with 242 additions and 1 deletions
243
cleanup.ps1
243
cleanup.ps1
|
|
@ -2,10 +2,13 @@
|
|||
# System Cleanup Script
|
||||
#================================================================================
|
||||
# Purpose: Automates system maintenance tasks including:
|
||||
# - Restore point creation
|
||||
# - Directory creation
|
||||
# - Quest Help installation
|
||||
# - Windows Defender exclusions
|
||||
# - Restore point configuration
|
||||
# - BleachBit cleanup
|
||||
# - System optimization
|
||||
# - Application updates
|
||||
# - Windows updates
|
||||
# - Structured webhook logging
|
||||
|
|
@ -86,7 +89,7 @@ if (-not (Test-Path $LogDirectory)) {
|
|||
# Initialize structured data object
|
||||
$ScriptResult = [ordered]@{
|
||||
"script_name" = "System Cleanup Script"
|
||||
"version" = "1.3"
|
||||
"version" = "1.4"
|
||||
"ticket_number" = $TicketNumber
|
||||
"webhook_enabled" = $SendWebhook
|
||||
"execution_info" = @{
|
||||
|
|
@ -98,6 +101,11 @@ $ScriptResult = [ordered]@{
|
|||
"serial_number" = (Get-CimInstance -ClassName Win32_BIOS).SerialNumber
|
||||
"log_file" = $LogFile
|
||||
}
|
||||
"restore_point_initial" = @{
|
||||
"created" = $false
|
||||
"name" = ""
|
||||
"error" = $null
|
||||
}
|
||||
"directory_check" = @{
|
||||
"path" = "C:\Quest"
|
||||
"exists" = $false
|
||||
|
|
@ -123,6 +131,22 @@ $ScriptResult = [ordered]@{
|
|||
"scheduled_task_updated" = $false
|
||||
"frequency_configured" = $false
|
||||
}
|
||||
"bleachbit_cleanup" = @{
|
||||
"downloaded" = $false
|
||||
"extracted" = $false
|
||||
"cleaners_run" = 0
|
||||
"cleaners_failed" = 0
|
||||
"errors" = @()
|
||||
}
|
||||
"system_optimization" = @{
|
||||
"chkdsk_run" = $false
|
||||
"chkdsk_result" = ""
|
||||
"sfc_run" = $false
|
||||
"sfc_result" = ""
|
||||
"dism_run" = $false
|
||||
"dism_result" = ""
|
||||
"errors" = @()
|
||||
}
|
||||
"app_updates" = @{
|
||||
"winget_available" = $false
|
||||
"timeout_seconds" = $WingetTimeout
|
||||
|
|
@ -201,6 +225,30 @@ if (-not $SendWebhook) {
|
|||
Write-Host "===========================================" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
|
||||
# --------------------------------------------------------------------------------
|
||||
# Create Initial Restore Point
|
||||
# --------------------------------------------------------------------------------
|
||||
Write-Log "================================================================================" -Level "INFO"
|
||||
Write-Log "SECTION: Initial Restore Point" -Level "INFO"
|
||||
Write-Log "================================================================================" -Level "INFO"
|
||||
|
||||
$RestorePointName = "SYSTEM-CLEANUP-BEFORE-$($TicketNumber -replace '[^A-Za-z0-9]', '')-$($ScriptStartTime.ToString('yyyyMMddHHmmss'))"
|
||||
|
||||
try {
|
||||
Write-Log "Creating initial restore point: $RestorePointName" -Level "INFO"
|
||||
Checkpoint-Computer -Description $RestorePointName -ErrorAction Stop
|
||||
$ScriptResult.restore_point_initial.created = $true
|
||||
$ScriptResult.restore_point_initial.name = $RestorePointName
|
||||
Write-Log "Initial restore point created successfully." -Level "INFO"
|
||||
} catch {
|
||||
$ErrorMsg = "Failed to create initial restore point: $($_.Exception.Message)"
|
||||
$ScriptResult.restore_point_initial.error = $ErrorMsg
|
||||
Write-Log $ErrorMsg -Level "ERROR"
|
||||
}
|
||||
|
||||
Write-Log "Initial restore point section completed." -Level "INFO"
|
||||
"" | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||
|
||||
# --------------------------------------------------------------------------------
|
||||
# Directory check and creation
|
||||
# --------------------------------------------------------------------------------
|
||||
|
|
@ -389,6 +437,192 @@ try {
|
|||
Write-Log "System Restore configuration completed." -Level "INFO"
|
||||
"" | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||
|
||||
# --------------------------------------------------------------------------------
|
||||
# BleachBit Cleanup
|
||||
# --------------------------------------------------------------------------------
|
||||
Write-Log "================================================================================" -Level "INFO"
|
||||
Write-Log "SECTION: BleachBit Cleanup" -Level "INFO"
|
||||
Write-Log "================================================================================" -Level "INFO"
|
||||
|
||||
# Define BleachBit cleaners
|
||||
$BleachBitCleaners = @(
|
||||
"adobe_reader.cache", "adobe_reader.mru", "adobe_reader.tmp",
|
||||
"amule.known_clients", "amule.known_files", "amule.logs", "amule.temp",
|
||||
"brave.cache",
|
||||
"chromium.cache", "chromium.vacuum",
|
||||
"deepscan.backup", "deepscan.ds_store", "deepscan.thumbs_db", "deepscan.tmp",
|
||||
"deepscan.vim_swap_root", "deepscan.vim_swap_user",
|
||||
"discord.cache", "discord.vacuum",
|
||||
"filezilla.mru",
|
||||
"firefox.cache", "firefox.crash_reports", "firefox.url_history",
|
||||
"flash.cache", "flash.cookies",
|
||||
"gimp.tmp",
|
||||
"google_chrome.cache", "google_earth.temporary_files", "google_toolbar.search_history",
|
||||
"gpodder.cache", "gpodder.logs",
|
||||
"hexchat.logs", "hippo_opensim_viewer.cache",
|
||||
"internet_explorer.cache",
|
||||
"java.cache",
|
||||
"libreoffice.history",
|
||||
"microsoft_edge.cache", "microsoft_office.debug_logs", "microsoft_office.mru",
|
||||
"midnightcommander.history", "miro.cache", "miro.logs",
|
||||
"octave.history", "openofficeorg.cache", "openofficeorg.recent_documents",
|
||||
"opera.cache",
|
||||
"paint.mru", "palemoon.cache", "pidgin.cache", "pidgin.logs",
|
||||
"realplayer.logs",
|
||||
"safari.cache", "screenlets.logs", "seamonkey.cache",
|
||||
"secondlife_viewer.Cache", "secondlife_viewer.Logs", "silverlight.temp",
|
||||
"skype.installers", "slack.cache", "smartftp.cache", "smartftp.log", "smartftp.mru",
|
||||
"system.recycle_bin", "system.tmp", "system.updates",
|
||||
"teamviewer.logs", "teamviewer.mru", "thunderbird.cache",
|
||||
"vim.history", "vlc.memory_dump", "vlc.mru", "vuze.cache", "vuze.logs", "vuze.stats", "vuze.temp",
|
||||
"warzone2100.logs", "waterfox.cache", "winamp.mru",
|
||||
"windows_defender.backup", "windows_defender.history", "windows_defender.logs",
|
||||
"windows_defender.quarantine", "windows_defender.temp",
|
||||
"windows_explorer.mru", "windows_explorer.run", "windows_explorer.search_history",
|
||||
"windows_explorer.shellbags", "windows_explorer.thumbnails",
|
||||
"windows_media_player.cache", "windows_media_player.mru",
|
||||
"winrar.history", "winrar.temp", "winzip.mru", "wordpad.mru",
|
||||
"yahoo_messenger.cache", "yahoo_messenger.chat_logs", "yahoo_messenger.logs",
|
||||
"zoom.cache", "zoom.logs"
|
||||
)
|
||||
|
||||
# Download BleachBit Portable
|
||||
Write-Log "Downloading BleachBit Portable..." -Level "INFO"
|
||||
try {
|
||||
$BleachBitUrl = "https://www.bleachbit.org/download/file/t?file=BleachBit-5.0.2-portable.zip"
|
||||
$BleachBitZip = "C:\Quest\BleachBitPortable.zip"
|
||||
|
||||
Invoke-WebRequest -Uri $BleachBitUrl -OutFile $BleachBitZip -UseBasicParsing -ErrorAction Stop
|
||||
$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"
|
||||
try {
|
||||
$BleachBitDest = "C:\Quest"
|
||||
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"
|
||||
}
|
||||
}
|
||||
|
||||
# Stop Edge processes
|
||||
Write-Log "Stopping Edge processes..." -Level "INFO"
|
||||
try {
|
||||
$EdgeProcesses = Get-Process -Name "*Edge*" -ErrorAction SilentlyContinue
|
||||
if ($EdgeProcesses) {
|
||||
$EdgeProcesses | Stop-Process -Force -ErrorAction Stop
|
||||
Write-Log "Edge processes stopped successfully." -Level "INFO"
|
||||
} else {
|
||||
Write-Log "No Edge processes running." -Level "INFO"
|
||||
}
|
||||
} catch {
|
||||
Write-Log "Warning: Could not stop Edge processes: $($_.Exception.Message)" -Level "WARNING"
|
||||
}
|
||||
|
||||
# Run BleachBit cleanup
|
||||
$BleachBitExe = "C:\Quest\portableApps\BleachBit-Portable\bleachbit_console.exe"
|
||||
if (Test-Path $BleachBitExe) {
|
||||
Write-Log "Running BleachBit cleanup with $($BleachBitCleaners.Count) cleaners..." -Level "INFO"
|
||||
|
||||
try {
|
||||
# Build cleaner arguments
|
||||
$CleanerArgs = $BleachBitCleaners -join " "
|
||||
|
||||
# Run BleachBit
|
||||
$BleachBitProcess = Start-Process -FilePath $BleachBitExe -ArgumentList "--clean $CleanerArgs" -NoNewWindow -Wait -PassThru -ErrorAction Stop
|
||||
|
||||
$ScriptResult.bleachbit_cleanup.cleaners_run = $BleachBitCleaners.Count
|
||||
Write-Log "BleachBit cleanup completed. Exit code: $($BleachBitProcess.ExitCode)" -Level "INFO"
|
||||
|
||||
# Optionally delete BleachBit after use
|
||||
try {
|
||||
Write-Log "Cleaning up BleachBit Portable files..." -Level "INFO"
|
||||
Remove-Item "C:\Quest\portableApps" -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 {
|
||||
$ErrorMsg = "Failed to run BleachBit cleanup: $($_.Exception.Message)"
|
||||
$ScriptResult.bleachbit_cleanup.errors += $ErrorMsg
|
||||
$ScriptResult.bleachbit_cleanup.cleaners_failed = $BleachBitCleaners.Count
|
||||
Write-Log $ErrorMsg -Level "ERROR"
|
||||
}
|
||||
} else {
|
||||
Write-Log "BleachBit executable not found at: $BleachBitExe" -Level "ERROR"
|
||||
}
|
||||
|
||||
Write-Log "BleachBit cleanup section completed." -Level "INFO"
|
||||
"" | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||
|
||||
# --------------------------------------------------------------------------------
|
||||
# System Optimization
|
||||
# --------------------------------------------------------------------------------
|
||||
Write-Log "================================================================================" -Level "INFO"
|
||||
Write-Log "SECTION: System Optimization" -Level "INFO"
|
||||
Write-Log "================================================================================" -Level "INFO"
|
||||
|
||||
# Run CHKDSK
|
||||
Write-Log "Running CHKDSK with /R option..." -Level "INFO"
|
||||
Write-Log "Note: CHKDSK may require a system reboot to complete." -Level "WARNING"
|
||||
try {
|
||||
$ChkdskResult = cmd /c "echo y | chkdsk /R" 2>&1 | Out-String
|
||||
$ScriptResult.system_optimization.chkdsk_run = $true
|
||||
$ScriptResult.system_optimization.chkdsk_result = $ChkdskResult.Trim()
|
||||
Write-Log "CHKDSK command executed." -Level "INFO"
|
||||
Write-Log "CHKDSK output: $ChkdskResult" -Level "INFO"
|
||||
} catch {
|
||||
$ErrorMsg = "Failed to run CHKDSK: $($_.Exception.Message)"
|
||||
$ScriptResult.system_optimization.errors += $ErrorMsg
|
||||
Write-Log $ErrorMsg -Level "ERROR"
|
||||
}
|
||||
|
||||
# Run SFC
|
||||
Write-Log "Running System File Checker (SFC /scannow)..." -Level "INFO"
|
||||
try {
|
||||
$SfcResult = sfc /scannow 2>&1 | Out-String
|
||||
$ScriptResult.system_optimization.sfc_run = $true
|
||||
$ScriptResult.system_optimization.sfc_result = $SfcResult.Trim()
|
||||
Write-Log "SFC scan completed." -Level "INFO"
|
||||
Write-Log "SFC output: $SfcResult" -Level "INFO"
|
||||
} catch {
|
||||
$ErrorMsg = "Failed to run SFC: $($_.Exception.Message)"
|
||||
$ScriptResult.system_optimization.errors += $ErrorMsg
|
||||
Write-Log $ErrorMsg -Level "ERROR"
|
||||
}
|
||||
|
||||
# Run DISM
|
||||
Write-Log "Running DISM restore health..." -Level "INFO"
|
||||
try {
|
||||
$DismResult = DISM /Online /Cleanup-Image /RestoreHealth 2>&1 | Out-String
|
||||
$ScriptResult.system_optimization.dism_run = $true
|
||||
$ScriptResult.system_optimization.dism_result = $DismResult.Trim()
|
||||
Write-Log "DISM restore health completed." -Level "INFO"
|
||||
Write-Log "DISM output: $DismResult" -Level "INFO"
|
||||
} catch {
|
||||
$ErrorMsg = "Failed to run DISM: $($_.Exception.Message)"
|
||||
$ScriptResult.system_optimization.errors += $ErrorMsg
|
||||
Write-Log $ErrorMsg -Level "ERROR"
|
||||
}
|
||||
|
||||
Write-Log "System optimization section completed." -Level "INFO"
|
||||
"" | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||
|
||||
# --------------------------------------------------------------------------------
|
||||
# Updating all apps (with timeout protection)
|
||||
# --------------------------------------------------------------------------------
|
||||
|
|
@ -673,6 +907,11 @@ Write-Log "Total Errors: $($ScriptResult.summary.total_errors)" -Level "INFO"
|
|||
Write-Log "Total Warnings: $($ScriptResult.summary.total_warnings)" -Level "INFO"
|
||||
Write-Log "Serial Number: $($ScriptResult.execution_info.serial_number)" -Level "INFO"
|
||||
Write-Log "Ticket Number: $TicketNumber" -Level "INFO"
|
||||
Write-Log "Initial Restore Point Created: $($ScriptResult.restore_point_initial.created)" -Level "INFO"
|
||||
if ($ScriptResult.restore_point_initial.name) {
|
||||
Write-Log "Restore Point Name: $($ScriptResult.restore_point_initial.name)" -Level "INFO"
|
||||
}
|
||||
Write-Log "BleachBit Cleaners Run: $($ScriptResult.bleachbit_cleanup.cleaners_run)" -Level "INFO"
|
||||
Write-Log "Webhook Sent: $(if ($ScriptResult.webhook.sent) { 'Yes' } else { 'No' })" -Level "INFO"
|
||||
if (-not $ScriptResult.webhook.sent -and $ScriptResult.webhook.skipped_reason) {
|
||||
Write-Log "Webhook Skipped Reason: $($ScriptResult.webhook.skipped_reason)" -Level "INFO"
|
||||
|
|
@ -691,6 +930,8 @@ Write-Host "Duration: $($ScriptResult.summary.total_duration_seconds) seconds" -
|
|||
Write-Host "Status: $($ScriptResult.summary.overall_status)" -ForegroundColor Green
|
||||
Write-Host "Serial Number: $($ScriptResult.execution_info.serial_number)" -ForegroundColor Cyan
|
||||
Write-Host "Ticket Number: $TicketNumber" -ForegroundColor Cyan
|
||||
Write-Host "Initial Restore Point: $($ScriptResult.restore_point_initial.name)" -ForegroundColor Cyan
|
||||
Write-Host "BleachBit Cleaners: $($ScriptResult.bleachbit_cleanup.cleaners_run)" -ForegroundColor Cyan
|
||||
Write-Host "Errors: $($ScriptResult.summary.total_errors)" -ForegroundColor $(if ($ScriptResult.summary.total_errors -gt 0) { "Red" } else { "Green" })
|
||||
Write-Host "Warnings: $($ScriptResult.summary.total_warnings)" -ForegroundColor $(if ($ScriptResult.summary.total_warnings -gt 0) { "Yellow" } else { "Green" })
|
||||
if ($ScriptResult.app_updates.timed_out) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue