Files
nodeagent/scripts/uninstall.ps1
T
jkooe b7f44f7a64 feat(scripts): na-gui 陈旧计划任务白名单清理(ARCH B-P1-6)
历史版本在被控端上注册过 na-gui 等旧名计划任务,漏删会留下「端口被占、
新版本起不来」的幽灵任务。误删用户自己的任务则是比漏删严重得多的
生产事故 —— 故只按白名单删。

- scripts/lib/scheduled-tasks.mjs(新):TASK_WHITELIST(nodeagent /
  na-gui / nodeagent-console,只增不改)+ planTaskCleanup 纯函数
  (查询结果注入,白名单外一律不删、精确匹配大小写敏感、重复名去重)。
- scripts/uninstall.ps1:卸载末尾按白名单逐个注销(-ccontains 存在性
  检查 + 动作进日志)。
- scripts/control.ps1:新增 cleanup 子命令(ValidateSet + Clear-StaleTasks),
  Uninstall-Agent 复用同一白名单;control.cmd 菜单加第 8 项
  (CRLF 安全插入,纯 ASCII 不变)。
- tests/unit/na-gui-cleanup.test.mjs(新,10 例):白名单命中才删 /
  白名单外零动作 / 空列表 / 重复名 / 脏输入 + ps1 源码守卫与跨端
  一致性守卫(白名单 mjs 与 ps1 漂移即红 —— 注入故障实测 2 处变红,
  恢复后全绿)。
2026-10-26 01:01:05 +08:00

50 lines
1.9 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<#
.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."