Add robust auto-update feature to all three scripts

This commit is contained in:
2025-09-13 14:41:19 -04:00
parent e8f1a60eb1
commit dc32d19a7b
3 changed files with 336 additions and 1 deletions

View File

@@ -22,8 +22,120 @@ if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdent
Write-Host "Running with administrator privileges." -ForegroundColor Green
# Check for updates from Gitea repository
Write-Host "Checking for updates..." -ForegroundColor Cyan
$baseUrl = "https://gitea.andrewspolytechnic.com"
$repoPath = "sandrews/windows-install"
$updateScriptDir = $PSScriptRoot
$lastUpdateCheck = $null
$updateCheckFile = Join-Path -Path $updateScriptDir -ChildPath "last_update_check.txt"
try {
# Check if we've checked for updates in the last 24 hours
if (Test-Path $updateCheckFile) {
$lastUpdateCheck = Get-Content $updateCheckFile | Get-Date
$timeSinceLastCheck = (Get-Date) - $lastUpdateCheck
if ($timeSinceLastCheck.TotalHours -lt 24) {
Write-Host "Last update check was less than 24 hours ago. Skipping update check." -ForegroundColor DarkGray
# Continue with the script
} else {
# Time to check for updates again
$checkForUpdates = $true
}
} else {
# First time running update check
$checkForUpdates = $true
}
if ($checkForUpdates) {
# Create a temporary directory to download files
$tempDir = Join-Path -Path $env:TEMP -ChildPath "windows-install-update"
if (Test-Path $tempDir) { Remove-Item $tempDir -Recurse -Force }
New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
# Try different URL patterns for Gitea zip download
$possibleUrls = @(
"$baseUrl/$repoPath/archive/master.zip",
"$baseUrl/$repoPath/archive/refs/heads/master.zip",
"$baseUrl/api/v1/repos/$repoPath/archive/master.zip"
)
$downloadSuccess = $false
$zipPath = Join-Path -Path $tempDir -ChildPath "update.zip"
$ProgressPreference = 'SilentlyContinue' # Hide progress bar to speed up download
foreach ($zipUrl in $possibleUrls) {
Write-Host "Trying to download updates from $zipUrl..." -ForegroundColor Cyan
try {
Invoke-WebRequest -Uri $zipUrl -OutFile $zipPath -UseBasicParsing -TimeoutSec 10
# Check if the download was successful (file exists and is not empty)
if (Test-Path $zipPath -and (Get-Item $zipPath).Length -gt 0) {
$downloadSuccess = $true
Write-Host "Download successful!" -ForegroundColor Green
break # Exit the loop if download was successful
}
} catch {
Write-Host "Failed with this URL: $_" -ForegroundColor DarkGray
# Continue to the next URL
}
}
if ($downloadSuccess) {
try {
# Extract the zip file
Write-Host "Extracting update package..." -ForegroundColor Cyan
Expand-Archive -Path $zipPath -DestinationPath $tempDir -Force
# Find the extracted directory
$extractedDir = Get-ChildItem -Path $tempDir -Directory | Select-Object -First 1
if ($extractedDir -and (Test-Path $extractedDir.FullName)) {
# Copy all files except .git directory to current location
Write-Host "Installing updates..." -ForegroundColor Cyan
Get-ChildItem -Path $extractedDir.FullName | Where-Object { $_.Name -ne ".git" } | ForEach-Object {
$destPath = Join-Path -Path $updateScriptDir -ChildPath $_.Name
Copy-Item -Path $_.FullName -Destination $destPath -Recurse -Force
}
# Record the update time
Get-Date -Format "yyyy-MM-dd HH:mm:ss" | Out-File -FilePath $updateCheckFile -Force
Write-Host "Update completed successfully!" -ForegroundColor Green
# Suggest restarting the script with the updated version
$restartChoice = Read-Host "The script has been updated. Would you like to restart the script to use the updated version? (Y/N)"
if ($restartChoice -eq "Y" -or $restartChoice -eq "y") {
Write-Host "Restarting script..." -ForegroundColor Cyan
Start-Process PowerShell -Verb RunAs -ArgumentList "-ExecutionPolicy Bypass -File `"$PSCommandPath`""
exit
}
} else {
Write-Host "Could not find extracted update directory." -ForegroundColor Yellow
}
} catch {
Write-Host "Error extracting or installing updates: $_" -ForegroundColor Yellow
}
} else {
Write-Host "Could not download updates from any of the tried URLs." -ForegroundColor Yellow
}
# Clean up
if (Test-Path $tempDir) {
try {
Remove-Item $tempDir -Recurse -Force -ErrorAction SilentlyContinue
} catch {
Write-Host "Could not clean up temp directory: $_" -ForegroundColor DarkGray
}
}
}
} catch {
Write-Host "Error checking for updates: $_" -ForegroundColor Yellow
Write-Host "Continuing with current version..." -ForegroundColor Yellow
}
[Environment]::UserName
$uid = $Env:UserName
$uid = $Env:UserName # Get current username for use in paths
reg.exe add "HKCU\Software\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32" /f /ve
Add-WindowsCapability -Online -Name 'OpenSSH.Client~~~~0.0.1.0'