Use New-Service for Windows service creation

Switch from sc.exe create to PowerShell's New-Service cmdlet for
creating the Windows service. New-Service provides better error
handling and is more reliable. Keep sc.exe only for configuring
service recovery options (restart on failure), which New-Service
doesn't support.

Related to #776
This commit is contained in:
rcourtman
2025-11-29 12:32:56 +00:00
parent dbf32baaa0
commit b6f0d74c55

View File

@@ -314,18 +314,18 @@ if (-not [string]::IsNullOrWhiteSpace($AgentId)) { $ServiceArgs += @("--agent-id
$BinPath = "`"$DestPath`" $($ServiceArgs -join ' ')"
# Create Service with error handling
$scOutput = sc.exe create $AgentName binPath= "$BinPath" start= auto displayname= "Pulse Unified Agent" 2>&1
if ($LASTEXITCODE -ne 0) {
Show-Error "Failed to create service '$AgentName'.`nsc.exe output: $scOutput"
# Create Service using New-Service (more reliable than sc.exe create)
try {
New-Service -Name $AgentName `
-BinaryPathName $BinPath `
-DisplayName "Pulse Unified Agent" `
-Description "Pulse Unified Agent for Host and Docker monitoring" `
-StartupType Automatic | Out-Null
Write-Host "Service created successfully" -ForegroundColor Green
} catch {
Show-Error "Failed to create service '$AgentName'.`nError: $_"
Exit 1
}
Write-Host "Service created successfully" -ForegroundColor Green
$scOutput = sc.exe description $AgentName "Pulse Unified Agent for Host and Docker monitoring" 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host "Warning: Failed to set service description: $scOutput" -ForegroundColor Yellow
}
$scOutput = sc.exe failure $AgentName reset= 86400 actions= restart/5000/restart/5000/restart/5000 2>&1
if ($LASTEXITCODE -ne 0) {