# Windows Service Installation Script # Run as Administrator $serviceName = "HomeServerAgent" $serviceDisplayName = "Home Server Agent" $serviceDescription = "Lightweight agent for managing game servers" $servicePath = "C:\Program Files\HomeServerAgent" $dockerComposePath = "$servicePath\docker-compose.yml" # Check if running as administrator if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Write-Warning "Please run this script as Administrator" exit 1 } # Create service directory if (!(Test-Path $servicePath)) { New-Item -ItemType Directory -Path $servicePath -Force } # Copy files to service directory Copy-Item -Path ".\*" -Destination $servicePath -Recurse -Force # Install NSSM (Non-Sucking Service Manager) if not present $nssmPath = "$servicePath\nssm.exe" if (!(Test-Path $nssmPath)) { Write-Host "Downloading NSSM..." Invoke-WebRequest -Uri "https://nssm.cc/release/nssm-2.24.zip" -OutFile "$servicePath\nssm.zip" Expand-Archive -Path "$servicePath\nssm.zip" -DestinationPath $servicePath Copy-Item -Path "$servicePath\nssm-2.24\win64\nssm.exe" -Destination $nssmPath Remove-Item -Path "$servicePath\nssm.zip" -Force Remove-Item -Path "$servicePath\nssm-2.24" -Recurse -Force } # Create service & $nssmPath install $serviceName "docker-compose" & $nssmPath set $serviceName AppDirectory $servicePath & $nssmPath set $serviceName AppParameters "up -d" & $nssmPath set $serviceName DisplayName $serviceDisplayName & $nssmPath set $serviceName Description $serviceDescription & $nssmPath set $serviceName Start SERVICE_AUTO_START & $nssmPath set $serviceName AppStopMethodConsole 30000 & $nssmPath set $serviceName AppStopMethodWindow 30000 & $nssmPath set $serviceName AppStopMethodThreads 30000 & $nssmPath set $serviceName AppKillProcessTree 1 # Set service to restart on failure & $nssmPath set $serviceName AppRestartDelay 10000 & $nssmPath set $serviceName AppNoConsole 1 Write-Host "Service installed successfully!" Write-Host "To start the service: Start-Service $serviceName" Write-Host "To stop the service: Stop-Service $serviceName" Write-Host "To remove the service: & '$nssmPath' remove $serviceName confirm"