# publish\undeploy.ps1 — Stop and remove the BCDL Windows service and deployed files # Usage: .\publish\undeploy.ps1 [-DeployPath "C:\etl\bcdl"] [-KeepFiles] param( [string]$DeployPath = "C:\etl\bcdl", [switch]$KeepFiles ) # Auto-elevate to Administrator if not already if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { $keepArg = if ($KeepFiles) { " -KeepFiles" } else { "" } Start-Process powershell.exe "-NoExit -NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -DeployPath `"$DeployPath`"$keepArg" -Verb RunAs exit } Set-StrictMode -Version Latest $ErrorActionPreference = "Stop" # Stop and remove the service $svc = Get-Service -Name "bcdl-web" -ErrorAction SilentlyContinue if ($null -ne $svc) { if ($svc.Status -ne "Stopped") { Write-Host "Stopping bcdl-web service..." -ForegroundColor Cyan Stop-Service -Name "bcdl-web" -NoWait -Force -ErrorAction SilentlyContinue $deadline = (Get-Date).AddSeconds(20) while ((Get-Service -Name "bcdl-web").Status -ne "Stopped" -and (Get-Date) -lt $deadline) { Start-Sleep -Milliseconds 500 } if ((Get-Service -Name "bcdl-web").Status -ne "Stopped") { Write-Host "Service did not stop in time, killing caddy.exe..." -ForegroundColor Yellow taskkill /F /IM caddy.exe 2>$null Start-Sleep -Seconds 2 } } Write-Host "Removing bcdl-web service..." -ForegroundColor Cyan sc.exe delete "bcdl-web" Write-Host "Service removed." -ForegroundColor Green } else { Write-Host "Service 'bcdl-web' not found, skipping." -ForegroundColor Yellow } # Remove deployed files unless -KeepFiles was specified if (-not $KeepFiles) { if (Test-Path "$DeployPath\dist") { Write-Host "Removing $DeployPath\dist..." -ForegroundColor Cyan Remove-Item "$DeployPath\dist" -Recurse -Force } if (Test-Path "$DeployPath\Caddyfile") { Write-Host "Removing $DeployPath\Caddyfile..." -ForegroundColor Cyan Remove-Item "$DeployPath\Caddyfile" -Force } Write-Host "Deployed files removed." -ForegroundColor Green } else { Write-Host "Skipping file removal (-KeepFiles specified)." -ForegroundColor Yellow } Write-Host "Uninstall complete." -ForegroundColor Green