#Requires -RunAsAdministrator <# .SYNOPSIS Install hbc_windows.exe as a Windows Service using NSSM. .DESCRIPTION Installs the HeartBeat Client as a Windows Service that starts automatically. Requires NSSM (Non-Sucking Service Manager) in PATH or alongside this script. Requires hbc_windows.exe built via: pyinstaller hbc_windows.spec .PARAMETER Server HBD server hostname or IP address (required). .PARAMETER ExePath Path to hbc_windows.exe. Defaults to the directory containing this script. .PARAMETER ServiceName Windows service name. Default: heartbeat-client .PARAMETER ConfigFile Path to hbc.json config file. Optional. .PARAMETER LogFile Path to log file. Default: C:\ProgramData\heartbeat\hbc.log .PARAMETER Interval Heartbeat interval in seconds. Default: 10 .EXAMPLE .\install_hbc_windows.ps1 -Server hbd.example.com .\install_hbc_windows.ps1 -Server hbd.example.com -ConfigFile C:\ProgramData\heartbeat\hbc.json #> param( [Parameter(Mandatory = $true)] [string]$Server, [string]$ExePath = "", [string]$ServiceName = "heartbeat-client", [string]$ConfigFile = "", [string]$LogFile = "C:\ProgramData\heartbeat\hbc.log", [int]$Interval = 10 ) Set-StrictMode -Version Latest $ErrorActionPreference = "Stop" # Locate hbc_windows.exe if ($ExePath -eq "") { $ExePath = Join-Path $PSScriptRoot "hbc_windows.exe" } if (-not (Test-Path $ExePath)) { Write-Error "hbc_windows.exe not found at: $ExePath`nBuild it first with: pyinstaller hbc_windows.spec" exit 1 } # Locate NSSM $nssm = Get-Command nssm -ErrorAction SilentlyContinue if (-not $nssm) { $nssmLocal = Join-Path $PSScriptRoot "nssm.exe" if (Test-Path $nssmLocal) { $nssm = $nssmLocal } else { Write-Error "nssm.exe not found in PATH or alongside this script.`nDownload from https://nssm.cc/download" exit 1 } } else { $nssm = $nssm.Source } # Build argument list $args_list = "--daemon $Server" if ($ConfigFile -ne "") { $args_list = "--daemon -c `"$ConfigFile`" $Server" } if ($LogFile -ne "") { $args_list = "$args_list --log-file `"$LogFile`"" } # Create data directory $dataDir = "C:\ProgramData\heartbeat" if (-not (Test-Path $dataDir)) { New-Item -ItemType Directory -Path $dataDir | Out-Null Write-Host "Created $dataDir" } # Remove existing service if present $existing = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue if ($existing) { Write-Host "Removing existing service '$ServiceName'..." & $nssm stop $ServiceName 2>$null & $nssm remove $ServiceName confirm } # Install service Write-Host "Installing service '$ServiceName'..." & $nssm install $ServiceName $ExePath $args_list if ($LASTEXITCODE -ne 0) { Write-Error "nssm install failed (exit $LASTEXITCODE)" exit 1 } # Configure service & $nssm set $ServiceName DisplayName "HeartBeat Client" & $nssm set $ServiceName Description "Sends heartbeat and plugin metrics to the HBD monitoring server." & $nssm set $ServiceName Start SERVICE_AUTO_START & $nssm set $ServiceName AppStdout (Join-Path $dataDir "nssm_stdout.log") & $nssm set $ServiceName AppStderr (Join-Path $dataDir "nssm_stderr.log") & $nssm set $ServiceName AppRotateFiles 1 & $nssm set $ServiceName AppRotateBytes 5242880 # Start service Write-Host "Starting service '$ServiceName'..." & $nssm start $ServiceName if ($LASTEXITCODE -ne 0) { Write-Warning "Service installed but failed to start — check logs in $dataDir" } else { Write-Host "Service '$ServiceName' started successfully." Write-Host "Log file: $LogFile" Write-Host "" Write-Host "Useful commands:" Write-Host " nssm status $ServiceName" Write-Host " nssm stop $ServiceName" Write-Host " nssm restart $ServiceName" Write-Host " nssm remove $ServiceName confirm" }