Files

24 lines
1.6 KiB
PowerShell

[CmdletBinding()]
param([switch]$Plan)
$ErrorActionPreference = 'Stop'
$serviceName = 'HomeVisionPower'
$dataRoot = Join-Path $env:ProgramData $serviceName
$scExe = Join-Path $env:WINDIR 'System32\sc.exe'
if ($Plan) {
Write-Output 'Stop/disable HomeVisionPower, retain maintenance.lock, remove only its SeShutdownPrivilege assignment, delete service registration; retain binary, DPAPI config and journal.'
return
}
$principal = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
if (!$principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { throw 'Run the explicit uninstaller in an elevated PowerShell.' }
$service = Get-Service -Name $serviceName -ErrorAction Stop
Set-Service -Name $serviceName -StartupType Disabled
Stop-Service -Name $serviceName -ErrorAction Stop
$service.WaitForStatus([ServiceProcess.ServiceControllerStatus]::Stopped, [TimeSpan]::FromSeconds(30))
if (Test-Path -LiteralPath $dataRoot -PathType Container) { [IO.File]::WriteAllText((Join-Path $dataRoot 'maintenance.lock'), 'Service uninstalled; safety lock retained.') }
$serviceSid = ([Security.Principal.NTAccount]"NT SERVICE\$serviceName").Translate([Security.Principal.SecurityIdentifier])
Add-Type -Path (Join-Path $PSScriptRoot 'ServiceRights.cs')
[HomeVisionServiceRights]::SetShutdownRight($serviceSid, $false)
& $scExe delete $serviceName
if ($LASTEXITCODE -ne 0) { throw 'Service registration deletion failed.' }
Write-Output 'Service removed. Binary, protected configuration and consumed-command journal were retained for recovery/audit.'