param (
[switch]$Elevated
)
$ErrorActionPreference = "Stop"
try {
Write-Host "Beszel Agent Upgrade Wrapper" -ForegroundColor Cyan
Write-Host "============================" -ForegroundColor Cyan
Write-Host ""
$scriptUrl = "https://raw.githubusercontent.com/henrygd/beszel/main/supplemental/scripts/upgrade-agent.ps1"
$tempScriptPath = "$env:TEMP\beszel-upgrade-agent-$(Get-Date -Format 'yyyyMMdd-HHmmss').ps1"
Write-Host "Downloading latest upgrade script..." -ForegroundColor Yellow
Write-Host "From: $scriptUrl"
Write-Host "To: $tempScriptPath"
try {
Invoke-WebRequest -Uri $scriptUrl -OutFile $tempScriptPath -UseBasicParsing
Write-Host "Download completed successfully." -ForegroundColor Green
}
catch {
Write-Host "Failed to download upgrade script: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "Please check your internet connection and try again." -ForegroundColor Red
exit 1
}
if (-not (Test-Path $tempScriptPath)) {
Write-Host "ERROR: Downloaded script not found at $tempScriptPath" -ForegroundColor Red
exit 1
}
Write-Host ""
Write-Host "Executing upgrade script..." -ForegroundColor Yellow
if ($Elevated) {
& $tempScriptPath -Elevated
} else {
& $tempScriptPath
}
$scriptExitCode = $LASTEXITCODE
Write-Host ""
Write-Host "Cleaning up temporary files..." -ForegroundColor Yellow
try {
Remove-Item $tempScriptPath -Force -ErrorAction SilentlyContinue
Write-Host "Cleanup completed." -ForegroundColor Green
}
catch {
Write-Host "Warning: Could not remove temporary script: $tempScriptPath" -ForegroundColor Yellow
}
exit $scriptExitCode
}
catch {
Write-Host "ERROR: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "Upgrade wrapper failed. Please check the error message above." -ForegroundColor Red
if ($tempScriptPath -and (Test-Path $tempScriptPath)) {
try {
Remove-Item $tempScriptPath -Force -ErrorAction SilentlyContinue
}
catch {
}
}
exit 1
}