修复 install-nssm-services.ps1:PS7 下首次安装报 NativeCommandError 中断

PowerShell 7.3+ 默认会把"原生命令写了 stderr 且退出码非 0"当作终止性错误抛出,
即使用 2>$null 重定向也无法拦截(触发条件是退出码而非 stderr 内容是否被消费)。
脚本里 nssm stop/remove 针对尚不存在的服务(首次安装场景)必然会返回非 0,
这是预期内可忽略的失败,此前会导致 $ErrorActionPreference = "Stop" 直接中断脚本。

修复:脚本顶部关闭 $PSNativeCommandUseErrorActionPreference。
This commit is contained in:
2026-09-11 13:54:58 +08:00
parent 097b16eb09
commit 6345c23f1f
+6
View File
@@ -14,6 +14,12 @@
$ErrorActionPreference = "Stop"
# PowerShell 7.3+ 默认会把"原生命令写了 stderr 且退出码非 0"当作终止性错误抛出
# (即使用 2>$null 重定向也无法拦截,因为触发条件是退出码而非 stderr 内容)。
# 下面 Install-NssmService 里首次安装时 `nssm stop`/`nssm remove` 针对尚不存在的
# 服务必然会失败,这是预期内的可忽略错误,因此关闭该行为。
$PSNativeCommandUseErrorActionPreference = $false
$Nssm = "nssm.exe" # 若 nssm 不在 PATH,改成绝对路径,如 "C:\tools\nssm-2.24\win64\nssm.exe"
$RepoRoot = Resolve-Path (Join-Path $PSScriptRoot "..\..")
$Powershell = (Get-Command powershell.exe).Source