<# .SYNOPSIS nodeagent - uninstall the Windows agent. .DESCRIPTION Stops and removes the scheduled task and the firewall rule. Keeps the config (~/.nodeagent/agent.json) unless -Purge is given. #> [CmdletBinding()] param( [int]$Port = 8765, [switch]$Purge ) $ErrorActionPreference = "SilentlyContinue" $isAdmin = ([Security.Principal.WindowsPrincipal] ` [Security.Principal.WindowsIdentity]::GetCurrent() ).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) if (-not $isAdmin) { Write-Host "[x] Please run as Administrator."; exit 1 } Write-Host "[*] Stopping and removing scheduled tasks (whitelist only)..." # B-P1-6 / T05:白名单清理。历史版本在被控端上注册过 na-gui 等旧名任务, # 漏删会留下「端口被占、新版本起不来」的幽灵任务。 # 铁律:只删白名单内**且实际存在**的任务,禁通配删除 —— 误删用户自己的 # 任务是比漏删严重得多的生产事故。-ccontains 为大小写敏感包含,与 # scripts/lib/scheduled-tasks.mjs 的 TASK_WHITELIST 精确匹配同口径 # (tests/unit/na-gui-cleanup.test.mjs 有跨端一致性守卫,白名单不许两边漂移)。 $taskWhitelist = @("nodeagent", "na-gui", "nodeagent-console") $existingTasks = Get-ScheduledTask | Select-Object -ExpandProperty TaskName foreach ($name in $taskWhitelist) { if ($existingTasks -ccontains $name) { Stop-ScheduledTask -TaskName $name Unregister-ScheduledTask -TaskName $name -Confirm:$false Write-Host " removed task: $name" } else { Write-Host " task not present: $name" } } Write-Host "[*] Removing firewall rule..." Get-NetFirewallRule -DisplayName "nodeagent (TCP $Port)" | Remove-NetFirewallRule if ($Purge) { Write-Host "[*] Purging config..." Remove-Item -Recurse -Force (Join-Path $env:USERPROFILE ".nodeagent") } Write-Host "[+] Uninstalled."