-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuninstall.ps1
More file actions
136 lines (110 loc) · 4.67 KB
/
uninstall.ps1
File metadata and controls
136 lines (110 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
param (
[string]$ServiceName = "ProcessMonitorService"
)
#Requires -RunAsAdministrator
# PowerShell Version pruefen (falls gewuenscht)
if ($PSVersionTable.PSVersion.Major -lt 5) {
Write-Error "Dieses Skript erfordert mindestens PowerShell Version 5.0"
exit 1
}
# Funktion fuer sichere Service-Deinstallation
function Remove-WindowsService {
param (
[string]$Name
)
try {
$service = Get-Service -Name $Name -ErrorAction SilentlyContinue
if ($null -eq $service) {
Write-Host "Dienst '$Name' ist nicht installiert." -ForegroundColor Yellow
return $true
}
Write-Host "Stoppe Dienst '$Name'..." -ForegroundColor Yellow
# Dienst stoppen mit Timeout
if ($service.Status -eq 'Running') {
Stop-Service -Name $Name -Force -ErrorAction Stop
# Warten bis Dienst vollstaendig gestoppt ist
$timeout = 30
$timer = 0
do {
Start-Sleep -Seconds 1
$timer++
$service = Get-Service -Name $Name -ErrorAction SilentlyContinue
} while ($service.Status -ne 'Stopped' -and $timer -lt $timeout)
if ($service.Status -ne 'Stopped') {
throw "Dienst konnte nicht innerhalb von $timeout Sekunden gestoppt werden."
}
}
Write-Host "Loesche Dienst '$Name'..." -ForegroundColor Yellow
# Dienst loeschen
$result = & sc.exe delete $Name 2>&1
if ($LASTEXITCODE -ne 0) {
throw "Fehler beim Loeschen des Dienstes: $result"
}
Write-Host "Dienst '$Name' wurde erfolgreich entfernt." -ForegroundColor Green
return $true
}
catch {
Write-Host "Fehler beim Entfernen des Dienstes '$Name': $($_.Exception.Message)" -ForegroundColor Red
return $false
}
}
# Funktion fuer automatischen Administrator-Neustart
function Start-AsAdministrator {
try {
Write-Host "Starte PowerShell mit Administrator-Rechten neu..." -ForegroundColor Yellow
# Parameter fuer Neustart zusammenstellen (PS 5.1 kompatibel)
$arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$($MyInvocation.MyCommand.Path)`""
# Urspruengliche Parameter hinzufuegen
foreach ($param in $PSBoundParameters.GetEnumerator()) {
$arguments += " -$($param.Key) `"$($param.Value)`""
}
Start-Process -FilePath "powershell.exe" -Verb RunAs -ArgumentList $arguments
exit 0
}
catch {
Write-Host "Fehler beim Neustart mit Administrator-Rechten: $($_.Exception.Message)" -ForegroundColor Red
return $false
}
}
# Hauptlogik
try {
Write-Host "=== Service Deinstallation ===" -ForegroundColor Cyan
Write-Host "Service: $ServiceName" -ForegroundColor White
Write-Host "PowerShell Version: $($PSVersionTable.PSVersion)" -ForegroundColor Gray
Write-Host ""
# Administrator-Rechte pruefen (redundant wegen #Requires, aber fuer bessere Fehlermeldung)
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "Administrator-Rechte erforderlich!" -ForegroundColor Red
Write-Host ""
Write-Host "Loesungsoptionen:" -ForegroundColor Yellow
Write-Host "- PowerShell als Administrator oeffnen" -ForegroundColor White
Write-Host "- Rechtsklick auf PowerShell → 'Als Administrator ausfuehren'" -ForegroundColor White
Write-Host ""
$restart = Read-Host "Automatisch mit Administrator-Rechten neu starten? (j/n)"
# PS 5.1 kompatible Regex-Alternative
if ($restart -eq 'j' -or $restart -eq 'J' -or $restart -eq 'y' -or $restart -eq 'Y') {
Start-AsAdministrator
}
throw "Deinstallation abgebrochen - Administrator-Rechte erforderlich."
}
Write-Host "Administrator-Rechte bestaetigt" -ForegroundColor Green
Write-Host ""
# Service deinstallieren
$success = Remove-WindowsService -Name $ServiceName
Write-Host ""
if ($success) {
Write-Host "=== Deinstallation erfolgreich abgeschlossen ===" -ForegroundColor Green
exit 0
}
else {
Write-Host "=== Deinstallation mit Fehlern beendet ===" -ForegroundColor Red
exit 1
}
}
catch {
Write-Host ""
Write-Host "Kritischer Fehler: $($_.Exception.Message)" -ForegroundColor Red
Write-Host ""
Write-Host "=== Deinstallation fehlgeschlagen ===" -ForegroundColor Red
exit 1
}