Public/ps1/Configuration/Service/Install-ApprxrService.ps1

    <#
    .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'
    #>

function Install-ApprxrService {
    [CmdletBinding()]
    param(
        [string]$Extension
    )
    $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)
    $serviceSuffix = if ($Extension) { "_$Extension" } else { '' }
    $ServiceName = "ApprxrService$serviceSuffix"
    $startCommand = "Start-Apprxr$serviceSuffix"
    $arguments = "-NoProfile -Command 'Import-Module ''$modulePath''; $startCommand'"
    & $nssmExe install $ServiceName $pwshPath $arguments
    if ($Extension) {
        Set-ApprxrConfigurationValue -Key 'ServiceExtension' -Value $Extension
    }
    Log-Apprxr "Service '$ServiceName' installed to run $startCommand from module using NSSM."
}