Public/ps1/Configuration/Service/Install-ApprxrService.ps1
|
function Install-ApprxrService { <# .SYNOPSIS Installs Apprxr as a Windows service using NSSM. .DESCRIPTION Downloads NSSM into the configuration folder if not present and configures a Windows service to run Apprxr using NSSM. .PARAMETER ServiceName The name of the Windows service to create (default: ApprxrService). .PARAMETER ScriptPath The path to the main Apprxr script to run as a service. .EXAMPLE Install-ApprxrService -ScriptPath 'C:\Path\To\Start-Apprxr.ps1' #> [CmdletBinding()] param() $configFolder = Get-ApprxrConfigurationFolder $nssmExe = Join-Path $configFolder 'nssm.exe' if (-not (Test-Path $nssmExe)) { Write-Host "Downloading NSSM..." -ForegroundColor Yellow $nssmUrl = 'https://nssm.cc/release/nssm-2.24.zip' $zipPath = Join-Path $configFolder 'nssm.zip' Invoke-WebRequest -Uri $nssmUrl -OutFile $zipPath Add-Type -AssemblyName System.IO.Compression.FileSystem [System.IO.Compression.ZipFile]::ExtractToDirectory($zipPath, $configFolder) Remove-Item $zipPath $nssmExeFound = Get-ChildItem -Path $configFolder -Recurse -Filter nssm.exe | Select-Object -First 1 if ($nssmExeFound) { Copy-Item $nssmExeFound.FullName $nssmExe -Force } else { throw "NSSM executable not found after extraction." } } $pwshPath = (Get-Command pwsh).Source $modulePath = Join-Path $PSScriptRoot '..' $modulePath = Join-Path $modulePath '..' $modulePath = Join-Path $modulePath '..' $modulePath = Join-Path $modulePath 'Apprxr.psm1' $modulePath = [System.IO.Path]::GetFullPath($modulePath) $arguments = "-NoProfile -Command 'Import-Module ''$modulePath''; Start-Apprxr'" $ServiceName = 'ApprxrService' & $nssmExe install $ServiceName $pwshPath $arguments Log-Apprxr "Service '$ServiceName' installed to run Start-Apprxr from module using NSSM." } |