63 lines
1.6 KiB
PowerShell
63 lines
1.6 KiB
PowerShell
#Requires -Version 5.1
|
|
#Requires -RunAsAdministrator
|
|
<#
|
|
.SYNOPSIS
|
|
Stops and removes the MailOrganizer Windows service via NSSM.
|
|
|
|
.PARAMETER ServiceName
|
|
Name of the Windows service (default: MailOrganizer)
|
|
|
|
.PARAMETER NssmPath
|
|
Path to nssm.exe (default: looks in PATH, then tools\nssm.exe)
|
|
|
|
.EXAMPLE
|
|
.\Uninstall-Service.ps1
|
|
.\Uninstall-Service.ps1 -ServiceName "MailOrganizer-Test"
|
|
#>
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
# --- Check if service exists ---
|
|
$existing = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
|
|
if (-not $existing) {
|
|
Write-Host "Service '$ServiceName' not found. Nothing to uninstall." -ForegroundColor Yellow
|
|
exit 0
|
|
}
|
|
|
|
# --- Stop service if running ---
|
|
if ($existing.Status -eq "Running") {
|
|
Write-Host "Stopping service '$ServiceName'..."
|
|
& $nssm stop $ServiceName
|
|
Start-Sleep -Seconds 2
|
|
}
|
|
|
|
# --- Remove service ---
|
|
Write-Host "Removing service '$ServiceName'..."
|
|
& $nssm remove $ServiceName confirm
|
|
if ($LASTEXITCODE -ne 0) { throw "NSSM remove failed" }
|
|
|
|
Write-Host "Service '$ServiceName' removed successfully." -ForegroundColor Green
|