# ============================================================= # GUARDiA ITSM Uninstall Script (Windows) # ============================================================= # GS Certification Portability > Installability requirement # Usage: .\uninstall.ps1 [-Purge] [-NoBackup] [-KeepJava] # ============================================================= param( [switch]$Purge = $false, [switch]$NoBackup = $false, [switch]$KeepJava = $false, [switch]$KeepDb = $false ) $ErrorActionPreference = "Continue" $LogFile = "C:\guardia_uninstall.log" $BackupDir = "C:\guardia_backup_$(Get-Date -Format 'yyyyMMdd_HHmmss')" function Write-OK { param($m) Write-Host "[OK] $m" -ForegroundColor Green; Add-Content $LogFile "[OK] $m" } function Write-Warn { param($m) Write-Host "[WARN] $m" -ForegroundColor Yellow; Add-Content $LogFile "[WARN] $m" } function Write-Info { param($m) Write-Host " $m"; Add-Content $LogFile " $m" } "=== GUARDiA ITSM Uninstall: $(Get-Date) ===" | Out-File $LogFile $isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) if (-not $isAdmin) { Write-Host "Administrator required." -ForegroundColor Red; exit 1 } $modeStr = if ($Purge) { "Full Purge" } else { "Standard" } Write-Host "==================================================" -ForegroundColor Cyan Write-Host " GUARDiA ITSM Uninstall - Mode: $modeStr" -ForegroundColor Cyan Write-Host "==================================================" -ForegroundColor Cyan # ── 1. Backup ───────────────────────────────────────────────── if (-not $NoBackup) { Write-Host "[1/6] Data backup..." New-Item -ItemType Directory -Force $BackupDir | Out-Null $dbFiles = Get-ChildItem "C:\GUARDiA\itsm" -Filter "*.db" -ErrorAction SilentlyContinue foreach ($db in $dbFiles) { Copy-Item $db.FullName "$BackupDir\" -Force Write-Info "DB backup: $($db.FullName)" } $envFile = "C:\GUARDiA\itsm\.env" if (Test-Path $envFile) { Copy-Item $envFile "$BackupDir\" -Force Write-Info ".env backup" } $uploadDir = "C:\GUARDiA\itsm\uploads" if (Test-Path $uploadDir) { Copy-Item $uploadDir "$BackupDir\uploads" -Recurse -Force Write-Info "Uploads backup" } Write-OK "Backup complete: $BackupDir" } # ── 2. Stop and remove NSSM services ────────────────────────── Write-Host "[2/6] Stopping GUARDiA services..." $services = @("guardia-itsm", "tomcat9", "ollama", "redis-server", "gitea") foreach ($svc in $services) { try { nssm stop $svc 2>$null } catch {} try { nssm remove $svc confirm 2>$null } catch {} $s = Get-Service $svc -ErrorAction SilentlyContinue if ($s) { try { Stop-Service $svc -Force } catch {} try { sc.exe delete $svc 2>$null } catch {} } Write-Info "$svc removed" } Write-OK "Services removed" # ── 3. Remove Ollama ────────────────────────────────────────── Write-Host "[3/6] Removing Ollama..." $ollamaExe = Get-Command ollama -ErrorAction SilentlyContinue if ($ollamaExe) { $ollamaDir = Split-Path (Split-Path $ollamaExe.Source) if ($Purge -and (Test-Path $ollamaDir)) { Remove-Item $ollamaDir -Recurse -Force -ErrorAction SilentlyContinue Write-Info "Ollama directory removed" } } if ($Purge) { Remove-Item "$env:LOCALAPPDATA\Ollama" -Recurse -Force -ErrorAction SilentlyContinue } Write-OK "Ollama removed" # ── 4. Remove Gitea ─────────────────────────────────────────── Write-Host "[4/6] Removing Gitea..." if ($Purge) { Remove-Item "C:\var\lib\gitea" -Recurse -Force -ErrorAction SilentlyContinue Remove-Item "C:\etc\gitea" -Recurse -Force -ErrorAction SilentlyContinue } Write-OK "Gitea removed" # ── 5. Remove GUARDiA files ─────────────────────────────────── Write-Host "[5/6] Removing GUARDiA files..." # Python virtual environment Remove-Item "C:\guardia\venv" -Recurse -Force -ErrorAction SilentlyContinue Write-Info "Python venv removed" if ($Purge) { # Full removal Remove-Item "C:\app\tomcat" -Recurse -Force -ErrorAction SilentlyContinue Remove-Item "C:\tools\maven" -Recurse -Force -ErrorAction SilentlyContinue Remove-Item "C:\guardia\logs" -Recurse -Force -ErrorAction SilentlyContinue Write-Info "Tomcat/Maven/Logs removed" if (-not $KeepJava) { # Remove JDK via winget try { winget uninstall --id Microsoft.OpenJDK.17 --silent 2>$null Write-Info "OpenJDK 17 removed" } catch {} } } else { # Keep data, remove source only Get-ChildItem "C:\GUARDiA\itsm" -Filter "*.py" -Recurse -ErrorAction SilentlyContinue | Remove-Item -Force Write-Info "Source files removed (data preserved)" } # Remove firewall rules Remove-NetFirewallRule -DisplayName "GUARDiA*" -ErrorAction SilentlyContinue Remove-NetFirewallRule -DisplayName "Block Ollama" -ErrorAction SilentlyContinue Remove-NetFirewallRule -DisplayName "Block Tomcat" -ErrorAction SilentlyContinue Write-Info "Firewall rules removed" # Remove environment variables [System.Environment]::SetEnvironmentVariable("JAVA_HOME", $null, "User") [System.Environment]::SetEnvironmentVariable("MAVEN_HOME", $null, "User") Write-Info "Environment variables cleaned" # Remove Nginx config $nginxConf = "C:\tools\nginx-winssl\conf\conf.d\guardia.conf" if (Test-Path $nginxConf) { Remove-Item $nginxConf -Force; Write-Info "Nginx config removed" } Write-OK "GUARDiA files removed" # ── 6. Database removal (purge only) ────────────────────────── if ($Purge -and -not $KeepDb) { Write-Host "[6/6] Removing database..." $pgBin = "" $pgDirs = Get-ChildItem "C:\Program Files\PostgreSQL" -Directory -ErrorAction SilentlyContinue | Sort-Object Name -Descending | Select-Object -First 1 if ($pgDirs) { $pgBin = "$($pgDirs.FullName)\bin" } if ($pgBin -and (Test-Path $pgBin)) { $env:PGPASSWORD = "postgres" & "$pgBin\psql.exe" -U postgres -c "DROP DATABASE IF EXISTS guardia;" 2>$null & "$pgBin\psql.exe" -U postgres -c "DROP USER IF EXISTS guardia;" 2>$null Write-Info "PostgreSQL DB/User removed" } Get-ChildItem "C:\" -Filter "*.db" -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.Name -like "guardia*" } | Remove-Item -Force Write-OK "Database removed" } else { Write-Host "[6/6] Database preserved (use -Purge to remove)" } # ── Summary ─────────────────────────────────────────────────── Write-Host "" Write-Host "==================================================" -ForegroundColor Green Write-OK "GUARDiA ITSM Uninstall Complete!" if (-not $NoBackup) { Write-Info "Backup: $BackupDir" } if (-not $Purge) { Write-Info "Data preserved at C:\GUARDiA (use -Purge to delete)" } Write-Info "Log: $LogFile" Write-Host "==================================================" -ForegroundColor Green