1. NSSM 下载 + 安装
# 1. 下载并解压 NSSM
$TempDir = "$env:TEMP\nssm_install"
$NssmZip = "$TempDir\nssm.zip"
New-Item -ItemType Directory -Path $TempDir -Force | Out-Null
Invoke-WebRequest -Uri "https://nssm.cc/release/nssm-2.24.zip" -OutFile $NssmZip
Expand-Archive -Path $NssmZip -DestinationPath $TempDir -Force
$NssmExe = Get-ChildItem $TempDir -Recurse -Include "nssm.exe" | Where-Object { $_.Directory.Name -match "win64" } | Select-Object -First 1
2. 以Syncthing 为例,注册为服务
使用everything搜索nssm.exe,把它复制到下面的路径就好。
下面的命令在管理员 PowerShell 中运行,使用win+x+a进入
# === 1. 设置路径 ===
$NssmPath = "D:\Workflow\AutoScripts\nssm\nssm.exe" # 请根据你实际解压位置修改
$SyncthingExe = "D:\Program Files\Syncthing\syncthing-windows-amd64-v2.0.11\syncthing.exe"
$SyncthingHome = "C:\Users\admin\.config\syncthing"# === 2. 验证文件存在 ===
if (-not (Test-Path $NssmPath)) {Write-Error "NSSM 未找到:$NssmPath"exit 1
}
if (-not (Test-Path $SyncthingExe)) {Write-Error "Syncthing 未找到:$SyncthingExe"exit 1
}# === 3. 安装服务(关键:必须在管理员 PowerShell 中运行)===
& $NssmPath install Syncthing $SyncthingExe "-no-browser" "-home=$SyncthingHome"# === 4. 配置服务 ===
& $NssmPath set Syncthing Start SERVICE_AUTO_START
& $NssmPath set Syncthing AppDirectory "D:\Program Files\Syncthing\syncthing-windows-amd64-v2.0.11"# === 5. 启动服务 ===
& $NssmPath start SyncthingWrite-Host "✅ Syncthing 服务已成功安装并启动!" -ForegroundColor Green