fix(scripts): update agent env vars on reinstall instead of skipping (#2107)

Co-authored-by: henrygd <hank@henrygd.me>
This commit is contained in:
Sven van Ginkel
2026-08-19 17:37:11 +02:00
committed by GitHub
parent aa1d67a122
commit ba90daf4d6
2 changed files with 109 additions and 19 deletions

View File

@@ -9,9 +9,21 @@ param (
[string]$NSSMPath = "",
[switch]$ConfigureFirewall,
[ValidateSet("Auto", "Scoop", "WinGet")]
[string]$InstallMethod = "Auto"
[string]$InstallMethod = "Auto",
# Set automatically from $PSBoundParameters below, or forwarded through an elevated relaunch.
# Used so a reinstall only overwrites Token/Url/Port on an existing service if the caller
# actually asked to change them, instead of wiping them with their unset defaults.
[switch]$TokenProvided,
[switch]$UrlProvided,
[switch]$PortProvided
)
if (-not $Elevated) {
$TokenProvided = $PSBoundParameters.ContainsKey('Token')
$UrlProvided = $PSBoundParameters.ContainsKey('Url')
$PortProvided = $PSBoundParameters.ContainsKey('Port')
}
# Check if required parameters are provided
if ([string]::IsNullOrWhiteSpace($Key)) {
Write-Host "ERROR: SSH Key is required." -ForegroundColor Red
@@ -312,7 +324,10 @@ function Install-NSSMService {
[string]$HubUrl = "",
[Parameter(Mandatory=$true)]
[int]$Port,
[string]$NSSMPath = ""
[string]$NSSMPath = "",
[switch]$TokenProvided,
[switch]$UrlProvided,
[switch]$PortProvided
)
Write-Host "Installing beszel-agent service..."
@@ -330,15 +345,26 @@ function Install-NSSMService {
$existingService = Get-Service -Name "beszel-agent" -ErrorAction SilentlyContinue
if ($existingService) {
Write-Host "Service already exists. Checking if path update is needed..."
# Get current service path
# Get current service path
$pathNeedsUpdate = $true
try {
$currentPath = & $nssmCommand get beszel-agent Application
if ($LASTEXITCODE -eq 0 -and $currentPath.Trim() -eq $AgentPath) {
Write-Host "Service already configured with correct path. Skipping service recreation." -ForegroundColor Green
Write-Host "Service path is already correct. Updating environment variables..."
& $nssmCommand set beszel-agent AppEnvironmentExtra "+KEY=$Key"
if ($TokenProvided) { & $nssmCommand set beszel-agent AppEnvironmentExtra "+TOKEN=$Token" }
if ($UrlProvided) { & $nssmCommand set beszel-agent AppEnvironmentExtra "+HUB_URL=$HubUrl" }
if ($PortProvided) { & $nssmCommand set beszel-agent AppEnvironmentExtra "+PORT=$Port" }
# Restart the service so the running process picks up the new environment variables
if ($existingService.Status -eq "Running") {
Write-Host "Restarting service to apply updated environment variables..."
& $nssmCommand restart beszel-agent
}
return
}
Write-Host "Service path needs updating. Stopping and removing existing service..."
Write-Host " Current path: $($currentPath.Trim())"
Write-Host " New path: $AgentPath"
@@ -346,7 +372,7 @@ function Install-NSSMService {
Write-Host "Could not retrieve current service path, will recreate service: $($_.Exception.Message)" -ForegroundColor Yellow
Write-Host "Service path needs updating. Stopping and removing existing service..."
}
try {
& $nssmCommand stop beszel-agent
& $nssmCommand remove beszel-agent confirm
@@ -589,6 +615,12 @@ try {
$argumentList += "`"$NSSMPath`""
}
# Forward which optional values were explicitly provided, so the elevated
# instance knows whether to overwrite them on an existing service
if ($TokenProvided) { $argumentList += "-TokenProvided" }
if ($UrlProvided) { $argumentList += "-UrlProvided" }
if ($PortProvided) { $argumentList += "-PortProvided" }
if ($ConfigureFirewall) {
$argumentList += "-ConfigureFirewall"
}
@@ -601,7 +633,7 @@ try {
# Third: If we have admin rights, install service and configure firewall
if ($isAdmin -or $Elevated) {
# Install the service
Install-NSSMService -AgentPath $AgentPath -Key $Key -Token $Token -HubUrl $Url -Port $Port -NSSMPath $NSSMPath
Install-NSSMService -AgentPath $AgentPath -Key $Key -Token $Token -HubUrl $Url -Port $Port -NSSMPath $NSSMPath -TokenProvided:$TokenProvided -UrlProvided:$UrlProvided -PortProvided:$PortProvided
if ($ConfigureFirewall) {
Configure-Firewall -Port $Port