#Requires -Version 5.1 #Requires -RunAsAdministrator <# .SYNOPSIS Installs the MailOrganizer Windows service via NSSM. .DESCRIPTION Registers a Windows service that runs Run-ServiceLoop.ps1 via NSSM. NSSM must be available in PATH or specified via -NssmPath. .PARAMETER ServiceName Name of the Windows service (default: MailOrganizer) .PARAMETER NssmPath Path to nssm.exe (default: looks in PATH, then tools\nssm.exe) .EXAMPLE .\Install-Service.ps1 .\Install-Service.ps1 -ServiceName "MailOrganizer-Test" -NssmPath "C:\tools\nssm.exe" #> param( [string]$ServiceName = "MailOrganizer", [string]$NssmPath ) $ErrorActionPreference = "Stop" # --- Locate NSSM --- if ($NssmPath -and (Test-Path $NssmPath)) { $nssm = $NssmPath } elseif (Get-Command nssm -ErrorAction SilentlyContinue) { $nssm = (Get-Command nssm).Source } else { $toolsPath = Join-Path (Split-Path $PSScriptRoot -Parent) "tools\nssm.exe" if (Test-Path $toolsPath) { $nssm = $toolsPath } else { Write-Host "ERROR: NSSM not found. Provide -NssmPath, add to PATH, or place in tools\nssm.exe" -ForegroundColor Red exit 1 } } Write-Host "Using NSSM: $nssm" # --- Check if service already exists --- $existing = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue if ($existing) { Write-Host "ERROR: Service '$ServiceName' already exists (Status: $($existing.Status)). Uninstall first." -ForegroundColor Red exit 1 } # --- Paths --- $scriptDir = $PSScriptRoot $projectRoot = Split-Path $scriptDir -Parent $loopScript = Join-Path $scriptDir "Run-ServiceLoop.ps1" $logDir = Join-Path $projectRoot "logs" if (-not (Test-Path $logDir)) { New-Item -ItemType Directory -Path $logDir -Force | Out-Null } $powershell = Join-Path $env:SystemRoot "System32\WindowsPowerShell\v1.0\powershell.exe" # --- Install service --- Write-Host "Installing service '$ServiceName'..." & $nssm install $ServiceName $powershell "-ExecutionPolicy Bypass -NoProfile -File `"$loopScript`"" if ($LASTEXITCODE -ne 0) { throw "NSSM install failed" } # Configure working directory & $nssm set $ServiceName AppDirectory $scriptDir # Configure logging $stdoutLog = Join-Path $logDir "service_stdout.log" $stderrLog = Join-Path $logDir "service_stderr.log" & $nssm set $ServiceName AppStdout $stdoutLog & $nssm set $ServiceName AppStderr $stderrLog & $nssm set $ServiceName AppStdoutCreationDisposition 4 & $nssm set $ServiceName AppStderrCreationDisposition 4 & $nssm set $ServiceName AppRotateFiles 1 & $nssm set $ServiceName AppRotateOnline 1 & $nssm set $ServiceName AppRotateBytes 5242880 # Set startup type to Automatic & $nssm set $ServiceName Start SERVICE_AUTO_START # Set description & $nssm set $ServiceName Description "Mail-Organizer: Processes emails and files attachments to SharePoint" Write-Host "" Write-Host "Service '$ServiceName' installed successfully." -ForegroundColor Green Write-Host " Stdout log: $stdoutLog" Write-Host " Stderr log: $stderrLog" Write-Host "" Write-Host "To start: Start-Service $ServiceName" Write-Host "To check: Get-Service $ServiceName"