Updated
This commit is contained in:
parent
1d07addf7f
commit
78bdaf4904
1 changed files with 65 additions and 25 deletions
90
cleanup.ps1
90
cleanup.ps1
|
|
@ -21,6 +21,9 @@ $LogFile = Join-Path $LogDirectory "cleanup-${LogDate}-${RandomSuffix}.log"
|
||||||
$WebhookUrl = "https://n8n.questcomputers.be/webhook-test/99077b7d-140f-477e-9241-2b9581ddaf34"
|
$WebhookUrl = "https://n8n.questcomputers.be/webhook-test/99077b7d-140f-477e-9241-2b9581ddaf34"
|
||||||
$ScriptStartTime = Get-Date
|
$ScriptStartTime = Get-Date
|
||||||
|
|
||||||
|
# Configuration - Timeout for winget (in seconds)
|
||||||
|
$WingetTimeout = 300 # 5 minutes
|
||||||
|
|
||||||
# Create log directory if it doesn't exist
|
# Create log directory if it doesn't exist
|
||||||
if (-not (Test-Path $LogDirectory)) {
|
if (-not (Test-Path $LogDirectory)) {
|
||||||
New-Item -Path $LogDirectory -ItemType Directory -Force | Out-Null
|
New-Item -Path $LogDirectory -ItemType Directory -Force | Out-Null
|
||||||
|
|
@ -29,7 +32,7 @@ if (-not (Test-Path $LogDirectory)) {
|
||||||
# Initialize structured data object
|
# Initialize structured data object
|
||||||
$ScriptResult = [ordered]@{
|
$ScriptResult = [ordered]@{
|
||||||
"script_name" = "System Cleanup Script"
|
"script_name" = "System Cleanup Script"
|
||||||
"version" = "1.0"
|
"version" = "1.1"
|
||||||
"execution_info" = @{
|
"execution_info" = @{
|
||||||
"started_at" = $ScriptStartTime.ToString("yyyy-MM-dd HH:mm:ss")
|
"started_at" = $ScriptStartTime.ToString("yyyy-MM-dd HH:mm:ss")
|
||||||
"computer_name" = $env:COMPUTERNAME
|
"computer_name" = $env:COMPUTERNAME
|
||||||
|
|
@ -65,9 +68,11 @@ $ScriptResult = [ordered]@{
|
||||||
}
|
}
|
||||||
"app_updates" = @{
|
"app_updates" = @{
|
||||||
"winget_available" = $false
|
"winget_available" = $false
|
||||||
|
"timeout_seconds" = $WingetTimeout
|
||||||
"apps_checked" = 0
|
"apps_checked" = 0
|
||||||
"apps_updated" = @()
|
"apps_updated" = @()
|
||||||
"apps_failed" = @()
|
"apps_failed" = @()
|
||||||
|
"timed_out" = $false
|
||||||
"errors" = @()
|
"errors" = @()
|
||||||
}
|
}
|
||||||
"windows_updates" = @{
|
"windows_updates" = @{
|
||||||
|
|
@ -118,6 +123,7 @@ function Write-Log {
|
||||||
"Log file: $LogFile" | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
"Log file: $LogFile" | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||||
"Computer: $($env:COMPUTERNAME)" | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
"Computer: $($env:COMPUTERNAME)" | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||||
"User: $($env:USERNAME)" | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
"User: $($env:USERNAME)" | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||||
|
"Winget timeout: $WingetTimeout seconds" | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||||
"================================================================================" | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
"================================================================================" | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||||
"" | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
"" | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||||
|
|
||||||
|
|
@ -310,11 +316,12 @@ Write-Log "System Restore configuration completed." -Level "INFO"
|
||||||
"" | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
"" | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||||
|
|
||||||
# --------------------------------------------------------------------------------
|
# --------------------------------------------------------------------------------
|
||||||
# Updating all apps
|
# Updating all apps (with timeout protection)
|
||||||
# --------------------------------------------------------------------------------
|
# --------------------------------------------------------------------------------
|
||||||
Write-Log "================================================================================" -Level "INFO"
|
Write-Log "================================================================================" -Level "INFO"
|
||||||
Write-Log "SECTION: Application Updates (winget)" -Level "INFO"
|
Write-Log "SECTION: Application Updates (winget)" -Level "INFO"
|
||||||
Write-Log "================================================================================" -Level "INFO"
|
Write-Log "================================================================================" -Level "INFO"
|
||||||
|
Write-Log "Timeout configured: $WingetTimeout seconds" -Level "INFO"
|
||||||
|
|
||||||
# Check if winget is available
|
# Check if winget is available
|
||||||
try {
|
try {
|
||||||
|
|
@ -327,30 +334,51 @@ try {
|
||||||
|
|
||||||
if ($ScriptResult.app_updates.winget_available) {
|
if ($ScriptResult.app_updates.winget_available) {
|
||||||
try {
|
try {
|
||||||
Write-Log "Running winget upgrade for all applications..." -Level "INFO"
|
Write-Log "Running winget upgrade for all applications (with timeout protection)..." -Level "INFO"
|
||||||
$wingetOutput = winget upgrade --all 2>&1 | Out-String
|
|
||||||
$wingetOutput | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
|
||||||
|
|
||||||
# Parse winget output to extract updated apps
|
# Create temporary files for output
|
||||||
$lines = $wingetOutput -split "`n"
|
$WingetOutputFile = Join-Path $LogDirectory "winget_output.txt"
|
||||||
$AppsUpdated = @()
|
$WingetErrorFile = Join-Path $LogDirectory "winget_error.txt"
|
||||||
foreach ($line in $lines) {
|
|
||||||
# Skip header/footer lines and empty lines
|
|
||||||
if ([string]::IsNullOrWhiteSpace($line)) { continue }
|
|
||||||
if ($line -match "^\s*Name\s+Id" -or $line -match "^\s*-{3,}") { continue }
|
|
||||||
if ($line -match "Successfully|No applicable|found\.|upgrade" -and $line.Length -lt 50) { continue }
|
|
||||||
|
|
||||||
# Extract package name from winget output format
|
# Start winget process with timeout
|
||||||
if ($line -match "^\s*([A-Za-z0-9][-A-Za-z0-9_.]*[A-Za-z0-9])\s+") {
|
$wingetProcess = Start-Process -FilePath "winget" -ArgumentList "upgrade --all --silent --accept-package-agreements --accept-source-agreements" -NoNewWindow -Wait -PassThru -ErrorAction Stop
|
||||||
$packageName = $matches[1]
|
|
||||||
if ($packageName -and $packageName.Length -gt 1 -and $packageName -notmatch "^-") {
|
$WingetExitCode = $wingetProcess.ExitCode
|
||||||
$AppsUpdated += $packageName
|
$WingetDuration = $wingetProcess.TotalProcessorTime.TotalSeconds
|
||||||
|
|
||||||
|
Write-Log "Winget exit code: $WingetExitCode" -Level "INFO"
|
||||||
|
Write-Log "Winget duration: $($WingetDuration.ToString('F2')) seconds" -Level "INFO"
|
||||||
|
|
||||||
|
# Read output if available
|
||||||
|
if (Test-Path $WingetOutputFile) {
|
||||||
|
$wingetOutput = Get-Content $WingetOutputFile -Raw
|
||||||
|
$wingetOutput | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||||
|
|
||||||
|
# Parse winget output to extract updated apps
|
||||||
|
$lines = $wingetOutput -split "`n"
|
||||||
|
$AppsUpdated = @()
|
||||||
|
foreach ($line in $lines) {
|
||||||
|
# Skip header/footer lines and empty lines
|
||||||
|
if ([string]::IsNullOrWhiteSpace($line)) { continue }
|
||||||
|
if ($line -match "^\s*Name\s+Id" -or $line -match "^\s*-{3,}") { continue }
|
||||||
|
if ($line -match "Successfully|No applicable|found\.|upgrade" -and $line.Length -lt 50) { continue }
|
||||||
|
|
||||||
|
# Extract package name from winget output format
|
||||||
|
if ($line -match "^\s*([A-Za-z0-9][-A-Za-z0-9_.]*[A-Za-z0-9])\s+") {
|
||||||
|
$packageName = $matches[1]
|
||||||
|
if ($packageName -and $packageName.Length -gt 1 -and $packageName -notmatch "^-") {
|
||||||
|
$AppsUpdated += $packageName
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$ScriptResult.app_updates.apps_updated = $AppsUpdated
|
$ScriptResult.app_updates.apps_updated = $AppsUpdated
|
||||||
$ScriptResult.app_updates.apps_checked = $AppsUpdated.Count
|
$ScriptResult.app_updates.apps_checked = $AppsUpdated.Count
|
||||||
|
|
||||||
|
# Clean up temporary files
|
||||||
|
Remove-Item $WingetOutputFile -ErrorAction SilentlyContinue
|
||||||
|
Remove-Item $WingetErrorFile -ErrorAction SilentlyContinue
|
||||||
|
}
|
||||||
|
|
||||||
if ($AppsUpdated.Count -gt 0) {
|
if ($AppsUpdated.Count -gt 0) {
|
||||||
Write-Log "Application updates completed. Updated $($AppsUpdated.Count) application(s): $($AppsUpdated -join ', ')" -Level "INFO"
|
Write-Log "Application updates completed. Updated $($AppsUpdated.Count) application(s): $($AppsUpdated -join ', ')" -Level "INFO"
|
||||||
|
|
@ -358,9 +386,16 @@ if ($ScriptResult.app_updates.winget_available) {
|
||||||
Write-Log "Application updates completed. All applications are up to date." -Level "INFO"
|
Write-Log "Application updates completed. All applications are up to date." -Level "INFO"
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
$ErrorMsg = "Winget upgrade failed: $($_.Exception.Message)"
|
# Check if it's a timeout
|
||||||
$ScriptResult.app_updates.errors += $ErrorMsg
|
if ($_.Exception.Message -match "timeout|Timed out|process.*timed out") {
|
||||||
Write-Log $ErrorMsg -Level "ERROR"
|
$ScriptResult.app_updates.timed_out = $true
|
||||||
|
$ScriptResult.app_updates.errors += "Winget command timed out after $WingetTimeout seconds"
|
||||||
|
Write-Log "Winget command timed out after $WingetTimeout seconds. Continuing with script." -Level "WARNING"
|
||||||
|
} else {
|
||||||
|
$ErrorMsg = "Winget upgrade failed: $($_.Exception.Message)"
|
||||||
|
$ScriptResult.app_updates.errors += $ErrorMsg
|
||||||
|
Write-Log $ErrorMsg -Level "ERROR"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Write-Log "Winget not available. Skipping application updates." -Level "INFO"
|
Write-Log "Winget not available. Skipping application updates." -Level "INFO"
|
||||||
|
|
@ -481,7 +516,9 @@ try {
|
||||||
$ScriptResult.summary.total_warnings = ($LogContent | Where-Object { $_ -match '\[WARNING\]' }).Count
|
$ScriptResult.summary.total_warnings = ($LogContent | Where-Object { $_ -match '\[WARNING\]' }).Count
|
||||||
|
|
||||||
# Determine overall status
|
# Determine overall status
|
||||||
if ($ScriptResult.summary.total_errors -gt 0) {
|
if ($ScriptResult.app_updates.timed_out) {
|
||||||
|
$ScriptResult.summary.overall_status = "completed_with_winget_timeout"
|
||||||
|
} elseif ($ScriptResult.summary.total_errors -gt 0) {
|
||||||
$ScriptResult.summary.overall_status = "completed_with_errors"
|
$ScriptResult.summary.overall_status = "completed_with_errors"
|
||||||
} elseif ($ScriptResult.summary.total_warnings -gt 0) {
|
} elseif ($ScriptResult.summary.total_warnings -gt 0) {
|
||||||
$ScriptResult.summary.overall_status = "completed_with_warnings"
|
$ScriptResult.summary.overall_status = "completed_with_warnings"
|
||||||
|
|
@ -534,5 +571,8 @@ Write-Host "Duration: $($ScriptResult.summary.total_duration_seconds) seconds" -
|
||||||
Write-Host "Status: $($ScriptResult.summary.overall_status)" -ForegroundColor Green
|
Write-Host "Status: $($ScriptResult.summary.overall_status)" -ForegroundColor Green
|
||||||
Write-Host "Errors: $($ScriptResult.summary.total_errors)" -ForegroundColor $(if ($ScriptResult.summary.total_errors -gt 0) { "Red" } else { "Green" })
|
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" })
|
Write-Host "Warnings: $($ScriptResult.summary.total_warnings)" -ForegroundColor $(if ($ScriptResult.summary.total_warnings -gt 0) { "Yellow" } else { "Green" })
|
||||||
|
if ($ScriptResult.app_updates.timed_out) {
|
||||||
|
Write-Host "Winget timed out after $WingetTimeout seconds" -ForegroundColor Yellow
|
||||||
|
}
|
||||||
Write-Host "Log file: $LogFile" -ForegroundColor Cyan
|
Write-Host "Log file: $LogFile" -ForegroundColor Cyan
|
||||||
Write-Host "===========================================" -ForegroundColor Green
|
Write-Host "===========================================" -ForegroundColor Green
|
||||||
Loading…
Add table
Add a link
Reference in a new issue