-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepair-WmiRepository.ps1
More file actions
443 lines (377 loc) · 18 KB
/
Copy pathRepair-WmiRepository.ps1
File metadata and controls
443 lines (377 loc) · 18 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# ====================================================================================
# Modernes WMI (Windows Management Instrumentation) Reparatur-Skript
# Autor: Gemini
# Version: 1.0 (PowerShell)
# Features: Transkript-Logging, Try/Catch-Fehlerbehandlung, PowerShell-Cmdlets
# ====================================================================================
# Danach optimiert mit Gemini und Claude. (back and forth)
# Version: 2.0 (PowerShell Optimiert)
# Features: Robuste Fehlerbehandlung, Backup-Validation, Service-Monitoring
# ACHTUNG: Als Administrator ausführen!
# ====================================================================================
<#
.SYNOPSIS
Repariert beschädigte Windows Management Instrumentation (WMI) Repositories und Services.
.DESCRIPTION
Dieses PowerShell-Skript führt eine umfassende WMI-Reparatur durch, die folgende Schritte umfasst:
• Stoppt abhängige Services (VSS, SMPHost, WinMgmt)
• Erstellt automatisch ein Backup des bestehenden WMI-Repositories
• Setzt das WMI-Repository zurück (winmgmt /resetrepository)
• Registriert alle System- und WMI-Provider-DLLs neu
• Kompiliert MOF/MFL-Dateien für WMI-Klassen
• Führt Konsistenzprüfungen durch
• Testet die WMI-Funktionalität nach der Reparatur
Das Skript protokolliert alle Aktionen in einer detaillierten Transkript-Datei und bietet
umfassende Fehlerbehandlung mit Backup-Wiederherstellungsoptionen.
.PARAMETER SkipBackup
Überspringt die Erstellung eines Repository-Backups. NICHT EMPFOHLEN für Produktionssysteme.
.PARAMETER Force
Überspringt alle Benutzerabfragen und führt die Reparatur automatisch durch.
Nützlich für unbeaufsichtigte Ausführung oder Skript-Integration.
.PARAMETER TimeoutSeconds
Maximale Wartezeit in Sekunden für Service-Operationen. Standard: 120 Sekunden.
.INPUTS
Keine Pipeline-Eingaben erforderlich.
.OUTPUTS
- Detaillierte Konsolen-Ausgabe mit Fortschrittsanzeigen
- Vollständiges Transkript-Log im TEMP-Verzeichnis
- Optional: Repository-Backup im WBEM-Verzeichnis
.EXAMPLE
PS C:\> .\Repair-WMI.ps1
Führt eine vollständige WMI-Reparatur mit Backup und Benutzerabfragen durch.
.EXAMPLE
PS C:\> .\Repair-WMI.ps1 -Force
Führt eine unbeaufsichtigte WMI-Reparatur ohne Benutzerabfragen durch.
.EXAMPLE
PS C:\> .\Repair-WMI.ps1 -SkipBackup -TimeoutSeconds 60
Führt eine WMI-Reparatur ohne Backup durch und verwendet kürzere Timeouts.
.NOTES
Dateiname: Repair-WMI.ps1
Autor: System Administrator
Erstellt: $(Get-Date -Format 'yyyy-MM-dd')
Version: 2.0
PowerShell: Requires 5.1 oder höher
Plattform: Windows 10/11, Windows Server 2016+
WICHTIGE HINWEISE:
• Erfordert Administrator-Rechte (automatisch geprüft)
• Erstellt automatisch Backups (außer bei -SkipBackup)
• Neustart nach Reparatur wird dringend empfohlen
• Bei kritischen Systemen vorher testen!
HÄUFIGE ANWENDUNGSFÄLLE:
• WMI-Abfragen schlagen fehl oder liefern keine Daten
• Group Policy Update-Probleme (gpupdate Fehler)
• SCCM/Intune Agent-Probleme
• PowerShell WMI-Cmdlet Fehler
• Monitoring-Tools können keine WMI-Daten abrufen
.LINK
https://docs.microsoft.com/en-us/windows/win32/wmisdk/
.LINK
https://docs.microsoft.com/en-us/troubleshoot/windows-client/admin-development/fix-wmi-corruption
#>
#Requires -RunAsAdministrator
[CmdletBinding()]
param(
[Parameter(HelpMessage = "Überspringt die Erstellung eines Repository-Backups (NICHT EMPFOHLEN)")]
[switch]$SkipBackup,
[Parameter(HelpMessage = "Überspringt alle Benutzerabfragen für unbeaufsichtigte Ausführung")]
[switch]$Force,
[Parameter(HelpMessage = "Maximale Wartezeit in Sekunden für Service-Operationen")]
[ValidateRange(30, 300)]
[int]$TimeoutSeconds = 120
)
# --- Skript-Konfiguration ---
$ErrorActionPreference = "Stop"
$LogFile = Join-Path -Path $env:TEMP -ChildPath "WMI-Repair-Log-$(Get-Date -Format 'yyyy-MM-dd_HH-mm-ss').txt"
$BackupPath = Join-Path -Path "$env:windir\System32\wbem" -ChildPath "Repository.bak-$(Get-Date -Format 'yyyy-MM-dd_HH-mm-ss')"
$RepositoryPath = Join-Path -Path "$env:windir\System32\wbem" -ChildPath "Repository"
$WbemPath = Join-Path -Path "$env:windir" -ChildPath "System32\wbem"
# --- Starte erweiterte Protokollierung ---
Start-Transcript -Path $LogFile -Force
Write-Host "========================================================" -ForegroundColor Cyan
Write-Host "WMI-Reparatur wird gestartet..." -ForegroundColor Green
Write-Host "Transkript: $LogFile" -ForegroundColor Gray
Write-Host "Computer: $env:COMPUTERNAME | Benutzer: $env:USERNAME" -ForegroundColor Gray
Write-Host "========================================================" -ForegroundColor Cyan
# --- Hilfsfunktionen ---
function Wait-ForServiceStatus {
param(
[string]$ServiceName,
[string]$DesiredStatus,
[int]$TimeoutSeconds = 60
)
$timeout = (Get-Date).AddSeconds($TimeoutSeconds)
Write-Verbose "Warte auf Service '$ServiceName' Status '$DesiredStatus'..."
do {
try {
$service = Get-Service -Name $ServiceName -ErrorAction Stop
if ($service.Status -eq $DesiredStatus) {
Write-Verbose "Service '$ServiceName' ist '$DesiredStatus'"
return $true
}
Start-Sleep -Seconds 1
}
catch {
Write-Warning "Service '$ServiceName' nicht gefunden oder nicht verfügbar."
return $false
}
} while ((Get-Date) -lt $timeout)
Write-Warning "Timeout: Service '$ServiceName' erreichte Status '$DesiredStatus' nicht innerhalb von $TimeoutSeconds Sekunden."
return $false
}
function Test-WMIFunctionality {
Write-Verbose "Teste WMI-Grundfunktionen..."
try {
# Teste sowohl CIM als auch traditionelle WMI-Zugriffe
$null = Get-CimInstance -ClassName Win32_ComputerSystem -ErrorAction Stop
Write-Verbose "CIM-Test erfolgreich"
# Zusätzlicher Test für traditionelle WMI-Kompatibilität
$null = Get-CimInstance -ClassName Win32_OperatingSystem -ErrorAction Stop
Write-Host " ✓ WMI/CIM-Test erfolgreich" -ForegroundColor Green
return $true
}
catch {
Write-Warning " ⚠ WMI/CIM-Test fehlgeschlagen: $($_.Exception.Message)"
return $false
}
}
# --- Hauptlogik mit umfassender Fehlerbehandlung ---
try {
# 0. Systemvoraussetzungen prüfen
Write-Host "[0/9] Prüfe Systemvoraussetzungen..." -ForegroundColor Cyan
if (-not (Test-Path $WbemPath)) {
throw "WMI-Verzeichnis '$WbemPath' nicht gefunden. System könnte beschädigt sein."
}
Write-Host " ✓ WMI-Verzeichnis gefunden" -ForegroundColor Green
# 1. Services stoppen und konfigurieren
Write-Host "[1/9] Stoppe und konfiguriere abhängige Services..." -ForegroundColor Cyan
$servicesToStop = @("vss", "smphost")
foreach ($serviceName in $servicesToStop) {
try {
$service = Get-Service -Name $serviceName -ErrorAction Stop
if ($service.Status -ne "Stopped") {
Write-Verbose "Stoppe Service '$serviceName'..."
Stop-Service -Name $serviceName -Force -ErrorAction Stop
Wait-ForServiceStatus -ServiceName $serviceName -DesiredStatus "Stopped" -TimeoutSeconds 30
}
Set-Service -Name $serviceName -StartupType Manual -ErrorAction Stop
Write-Host " ✓ Service '$serviceName' gestoppt und auf 'Manual' gesetzt" -ForegroundColor Green
}
catch {
Write-Warning " ⚠ Service '$serviceName': $($_.Exception.Message)"
}
}
# WMI-Service speziell behandeln
Write-Verbose "Stoppe WMI-Service (winmgmt)..."
try {
Set-Service -Name "winmgmt" -StartupType Disabled -ErrorAction Stop
Stop-Service -Name "winmgmt" -Force -ErrorAction Stop
$wmistopped = Wait-ForServiceStatus -ServiceName "winmgmt" -DesiredStatus "Stopped" -TimeoutSeconds 60
if (-not $wmistopped) {
Write-Warning " WMI-Service konnte nicht gestoppt werden. Fortfahren auf eigene Gefahr."
}
else {
Write-Host " ✓ WMI-Service erfolgreich gestoppt" -ForegroundColor Green
}
}
catch {
Write-Warning " WMI-Service Stopp-Fehler: $($_.Exception.Message)"
}
# 2. Backup des Repositorys (mit Validierung)
if (-not $SkipBackup) {
Write-Host "[2/9] Erstelle Backup des WMI-Repositorys..." -ForegroundColor Cyan
if (Test-Path $RepositoryPath) {
Write-Verbose "Backup läuft... (kann mehrere Minuten dauern)"
try {
# Robocopy für bessere Backup-Behandlung
$robocopyArgs = @(
"`"$RepositoryPath`"",
"`"$BackupPath`"",
"/E", "/COPYALL", "/R:1", "/W:1", "/NP"
)
$robocopyResult = & robocopy @robocopyArgs 2>&1
$robocopyExitCode = $LASTEXITCODE
if ($robocopyExitCode -le 1) {
# Backup-Validierung
$originalCount = (Get-ChildItem -Path $RepositoryPath -Recurse -File).Count
$backupCount = (Get-ChildItem -Path $BackupPath -Recurse -File).Count
if ($backupCount -ge ($originalCount * 0.95)) {
# 95% Toleranz
Write-Host " ✓ Backup erfolgreich erstellt: $BackupPath" -ForegroundColor Green
Write-Host " 📊 Dateien - Original: $originalCount, Backup: $backupCount" -ForegroundColor Gray
}
else {
throw "Backup-Validierung fehlgeschlagen. Original: $originalCount, Backup: $backupCount"
}
}
else {
throw "Robocopy fehlgeschlagen mit Exit-Code: $robocopyExitCode"
}
}
catch {
Write-Warning " ⚠ Backup-Fehler: $($_.Exception.Message)"
if (-not $Force) {
$continue = Read-Host "Trotzdem fortfahren? (Nicht empfohlen!) [j/N]"
if ($continue -ne 'j' -and $continue -ne 'J') {
throw "Abbruch durch Benutzer nach Backup-Fehler."
}
}
}
}
else {
Write-Warning " ⚠ Kein Repository-Ordner zum Sichern gefunden."
}
}
else {
Write-Host "[2/9] Backup übersprungen (Parameter -SkipBackup)" -ForegroundColor Yellow
}
# 3. Sicherheitsabfrage und Reset des Repositorys
Write-Host "[3/9] Setze WMI-Repository zurück..." -ForegroundColor Cyan
Write-Host " ⚠ ACHTUNG: Das WMI-Repository wird jetzt zurückgesetzt!" -ForegroundColor Red
if (Test-Path $BackupPath) {
Write-Host " 📁 Backup verfügbar: $BackupPath" -ForegroundColor Green
}
else {
Write-Host " ❌ KEIN BACKUP VERFÜGBAR!" -ForegroundColor Red
}
if (-not $Force) {
$choice = Read-Host " Fortfahren? [j/N]"
if ($choice -ne 'J' -and $choice -ne 'j') {
throw "Abbruch durch Benutzer."
}
}
Write-Verbose "Führe Repository-Reset durch..."
$resetProcess = Start-Process -FilePath "winmgmt" -ArgumentList "/resetrepository" -Wait -PassThru -WindowStyle Hidden
if ($resetProcess.ExitCode -eq 0) {
Write-Host " ✓ Repository erfolgreich zurückgesetzt" -ForegroundColor Green
}
else {
Write-Warning " ⚠ Repository-Reset Exit-Code: $($resetProcess.ExitCode) (kann normal sein)"
}
# 4. System-DLLs registrieren
Write-Host "[4/9] Registriere System-DLLs..." -ForegroundColor Cyan
$systemDlls = @(
"$env:windir\system32\scecli.dll",
"$env:windir\system32\userenv.dll"
)
foreach ($dll in $systemDlls) {
if (Test-Path $dll) {
Write-Verbose "Registriere $(Split-Path $dll -Leaf)..."
$regResult = Start-Process -FilePath "regsvr32" -ArgumentList "/s", "`"$dll`"" -Wait -PassThru -WindowStyle Hidden
if ($regResult.ExitCode -eq 0) {
Write-Host " ✓ $(Split-Path $dll -Leaf) registriert" -ForegroundColor Green
}
else {
Write-Warning " ⚠ Fehler bei $(Split-Path $dll -Leaf): Exit-Code $($regResult.ExitCode)"
}
}
else {
Write-Warning " ⚠ DLL nicht gefunden: $dll"
}
}
# 5. WMI-DLLs registrieren
Write-Host "[5/9] Registriere WMI-Provider-DLLs..." -ForegroundColor Cyan
$wmiDlls = Get-ChildItem -Path $WbemPath -Filter "*.dll" -ErrorAction SilentlyContinue
$dllCount = 0
$successCount = 0
foreach ($dll in $wmiDlls) {
$dllCount++
Write-Progress -Activity "Registriere WMI-DLLs" -Status $dll.Name -PercentComplete (($dllCount / $wmiDlls.Count) * 100)
$regResult = Start-Process -FilePath "regsvr32" -ArgumentList "/s", "`"$($dll.FullName)`"" -Wait -PassThru -WindowStyle Hidden
if ($regResult.ExitCode -eq 0) {
$successCount++
}
}
Write-Progress -Activity "Registriere WMI-DLLs" -Completed
Write-Host " ✓ $successCount von $dllCount DLLs erfolgreich registriert" -ForegroundColor Green
# 6. WMI-Dienst starten
Write-Host "[6/9] Starte WMI-Dienst..." -ForegroundColor Cyan
try {
Set-Service -Name "winmgmt" -StartupType Automatic -ErrorAction Stop
Start-Service -Name "winmgmt" -ErrorAction Stop
$wmiStarted = Wait-ForServiceStatus -ServiceName "winmgmt" -DesiredStatus "Running" -TimeoutSeconds $TimeoutSeconds
if (-not $wmiStarted) {
throw "WMI-Service konnte nicht gestartet werden."
}
Write-Host " ✓ WMI-Service erfolgreich gestartet" -ForegroundColor Green
}
catch {
throw "Kritischer Fehler beim Starten des WMI-Services: $($_.Exception.Message)"
}
# 7. MOF- und MFL-Dateien kompilieren
Write-Host "[7/9] Kompiliere MOF- und MFL-Dateien..." -ForegroundColor Cyan
# MOF-Dateien
$mofFiles = Get-ChildItem -Path $WbemPath -Filter "*.mof" -Recurse -ErrorAction SilentlyContinue |
Where-Object { $_.Name -notmatch "Uninstall|Remove|AutoRecover" }
# MFL-Dateien
$mflFiles = Get-ChildItem -Path $WbemPath -Filter "*.mfl" -Recurse -ErrorAction SilentlyContinue |
Where-Object { $_.Name -notmatch "Uninstall|Remove" }
$allFiles = @($mofFiles) + @($mflFiles)
$compiledCount = 0
$totalFiles = $allFiles.Count
Write-Verbose "Kompiliere $totalFiles Dateien..."
foreach ($file in $allFiles) {
$compiledCount++
Write-Progress -Activity "Kompiliere MOF/MFL-Dateien" -Status $file.Name -PercentComplete (($compiledCount / $totalFiles) * 100)
$compileResult = Start-Process -FilePath "mofcomp" -ArgumentList "`"$($file.FullName)`"" -Wait -PassThru -WindowStyle Hidden
if ($compileResult.ExitCode -ne 0) {
Write-Verbose " Warnung bei $($file.Name): Exit-Code $($compileResult.ExitCode)"
}
}
Write-Progress -Activity "Kompiliere MOF/MFL-Dateien" -Completed
Write-Host " ✓ $compiledCount Dateien kompiliert" -ForegroundColor Green
# 8. WMI-Konsistenz prüfen
Write-Host "[8/9] Überprüfe WMI-Konsistenz..." -ForegroundColor Cyan
$salvageResult = Start-Process -FilePath "winmgmt" -ArgumentList "/salvagerepository" -Wait -PassThru -WindowStyle Hidden
if ($salvageResult.ExitCode -eq 0) {
Write-Host " ✓ Konsistenzprüfung erfolgreich" -ForegroundColor Green
}
else {
Write-Warning " ⚠ Konsistenzprüfung Exit-Code: $($salvageResult.ExitCode)"
}
# 9. Finale Tests
Write-Host "[9/9] Führe WMI-Funktionstests durch..." -ForegroundColor Cyan
$wmiWorking = Test-WMIFunctionality
if ($wmiWorking) {
Write-Host " ✓ WMI-Funktionalität bestätigt" -ForegroundColor Green
}
else {
Write-Warning " ⚠ WMI-Funktionalität beeinträchtigt - Neustart erforderlich"
}
# --- Erfolgsmeldung ---
Write-Host "========================================================" -ForegroundColor Green
Write-Host "🎉 WMI-Reparatur erfolgreich abgeschlossen!" -ForegroundColor Green
Write-Host "📄 Vollständiges Transkript: $LogFile" -ForegroundColor Gray
if (Test-Path $BackupPath) {
Write-Host "💾 Repository-Backup: $BackupPath" -ForegroundColor Gray
}
Write-Host "========================================================" -ForegroundColor Green
# Neustart-Empfehlung
Write-Host "⚠ WICHTIG: Es wird dringend empfohlen, das System jetzt neu zu starten." -ForegroundColor Yellow
if (-not $Force) {
$restart = Read-Host "System jetzt neu starten? [j/N]"
if ($restart -eq 'j' -or $restart -eq 'J') {
Write-Host "Neustart wird in 10 Sekunden eingeleitet..." -ForegroundColor Yellow
Start-Sleep -Seconds 3
Restart-Computer -Force -Delay 10
}
}
}
catch {
Write-Host "========================================================" -ForegroundColor Red
Write-Error "❌ FEHLER: $($_.Exception.Message)"
Write-Host "Das Skript wurde abgebrochen." -ForegroundColor Red
if (Test-Path $BackupPath) {
Write-Host "💾 Repository-Backup verfügbar: $BackupPath" -ForegroundColor Yellow
Write-Host "Bei schwerwiegenden Problemen kann das Backup manuell wiederhergestellt werden." -ForegroundColor Yellow
}
Write-Host "📄 Vollständiges Fehler-Log: $LogFile" -ForegroundColor Gray
Write-Host "========================================================" -ForegroundColor Red
exit 1
}
finally {
# Protokollierung in jedem Fall beenden
Stop-Transcript
if (-not $Force) {
Read-Host "Drücke Enter zum Beenden"
}
}