37 lines
1.1 KiB
PowerShell
37 lines
1.1 KiB
PowerShell
[CmdletBinding(SupportsShouldProcess = $true)]
|
|
param(
|
|
[string]$ServiceName = "ScreenJobBackend"
|
|
)
|
|
|
|
Set-StrictMode -Version Latest
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
function Test-IsAdministrator {
|
|
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
|
|
$principal = New-Object Security.Principal.WindowsPrincipal($identity)
|
|
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
|
|
}
|
|
|
|
if (-not (Test-IsAdministrator)) {
|
|
throw "Run this script from an elevated PowerShell session (Run as Administrator)."
|
|
}
|
|
|
|
$service = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
|
|
if ($null -eq $service) {
|
|
Write-Host "Service '$ServiceName' is not installed."
|
|
exit 0
|
|
}
|
|
|
|
if ($PSCmdlet.ShouldProcess($ServiceName, "Uninstall service")) {
|
|
if ($service.Status -ne "Stopped") {
|
|
Stop-Service -Name $ServiceName -Force -ErrorAction Stop
|
|
}
|
|
|
|
& sc.exe delete $ServiceName | Out-Null
|
|
if ($LASTEXITCODE -ne 0) {
|
|
throw "Failed to delete service '$ServiceName' (sc.exe exit code $LASTEXITCODE)."
|
|
}
|
|
}
|
|
|
|
Write-Host "Service '$ServiceName' uninstalled successfully." -ForegroundColor Green
|