75 lines
2.1 KiB
PowerShell
75 lines
2.1 KiB
PowerShell
[CmdletBinding()]
|
|
param(
|
|
[Alias('keep-config')]
|
|
[switch]$KeepConfig
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
$ServiceName = "luxtools-client"
|
|
$TaskName = $ServiceName
|
|
|
|
function Write-Usage {
|
|
@"
|
|
Usage:
|
|
uninstall-windows-task.bat [--keep-config]
|
|
|
|
Uninstalls $ServiceName Scheduled Task and removes installed files.
|
|
|
|
Options:
|
|
--keep-config Keep %LOCALAPPDATA%\$ServiceName\config.json on disk.
|
|
"@ | Write-Host
|
|
}
|
|
|
|
# Allow -h/--help as positional args (PowerShell doesn't treat these as built-in).
|
|
foreach ($a in @($args)) {
|
|
if ($a -eq '-h' -or $a -eq '--help') {
|
|
Write-Usage
|
|
exit 0
|
|
}
|
|
}
|
|
|
|
$installDir = Join-Path $env:LOCALAPPDATA $ServiceName
|
|
$exePath = Join-Path $installDir "$ServiceName.exe"
|
|
$configPath = Join-Path $installDir "config.json"
|
|
|
|
# Stop task and any running instance (best-effort).
|
|
try { Stop-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue | Out-Null } catch {}
|
|
try { schtasks.exe /End /TN $TaskName 2>$null | Out-Null } catch {}
|
|
|
|
try {
|
|
$procs = Get-Process -Name $ServiceName -ErrorAction SilentlyContinue
|
|
foreach ($p in $procs) {
|
|
try {
|
|
if ($p.Path -and (Split-Path -Path $p.Path -Resolve) -ieq (Split-Path -Path $exePath -Resolve)) {
|
|
Stop-Process -Id $p.Id -Force -ErrorAction SilentlyContinue
|
|
}
|
|
} catch {}
|
|
}
|
|
} catch {}
|
|
|
|
# Remove task (best-effort).
|
|
try { Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false -ErrorAction SilentlyContinue | Out-Null } catch {}
|
|
try { schtasks.exe /Delete /TN $TaskName /F 2>$null | Out-Null } catch {}
|
|
|
|
# Remove files.
|
|
if (Test-Path -LiteralPath $installDir) {
|
|
if ($KeepConfig) {
|
|
# Remove everything except config.json
|
|
Get-ChildItem -LiteralPath $installDir -Force | ForEach-Object {
|
|
if ($_.FullName -ieq $configPath) { return }
|
|
try {
|
|
Remove-Item -LiteralPath $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
|
|
} catch {}
|
|
}
|
|
# If directory became empty except config, keep it.
|
|
} else {
|
|
try { Remove-Item -LiteralPath $installDir -Recurse -Force -ErrorAction SilentlyContinue } catch {}
|
|
}
|
|
}
|
|
|
|
Write-Host "Uninstalled $ServiceName."
|
|
if ($KeepConfig) {
|
|
Write-Host "Kept config file: $configPath"
|
|
}
|