-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest-PCActivityTimeline.ps1
More file actions
329 lines (289 loc) · 14.3 KB
/
Copy pathTest-PCActivityTimeline.ps1
File metadata and controls
329 lines (289 loc) · 14.3 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
#Requires -Version 5.1
<#
.SYNOPSIS
Diagnose-Script für Get-PCActivityTimeline.ps1
Prüft Konnektivität, Audit-Richtlinien, Event-Counts und
gibt rohe Properties[] der ersten Treffer jeder Event-ID aus —
damit können die Property-Indices im Haupt-Script verifiziert werden.
.USAGE
.\Test-PCActivityTimeline.ps1 -ComputerName "8A0228-D.mycorp.local"
.\Test-PCActivityTimeline.ps1 -ComputerName "8A0228-D.mycorp.local" -Credential (Get-Credential)
.\Test-PCActivityTimeline.ps1 -ComputerName "8A0228-D.mycorp.local" -AllePropertiesAusgeben
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)]
[string]$ComputerName,
[Parameter(Mandatory = $false)]
[pscredential]$Credential,
# Gibt für JEDEN gefundenen Event die vollständigen Properties aus
# (sonst nur der erste Treffer je EventID). Kann sehr viel Output erzeugen.
[Parameter(Mandatory = $false)]
[switch]$AllePropertiesAusgeben,
# Wie viele Tage rückwärts für die Diagnose-Abfragen
[Parameter(Mandatory = $false)]
[int]$TageRueckwaerts = 3
)
$SEP = '=' * 72
$sep2 = '-' * 72
function Write-Section {
param([string]$Title)
Write-Host $SEP -ForegroundColor DarkCyan
Write-Host " $Title" -ForegroundColor Cyan
Write-Host $SEP -ForegroundColor DarkCyan
}
function Write-OK { param([string]$Msg) Write-Host " [OK] $Msg" -ForegroundColor Green }
function Write-WARN { param([string]$Msg) Write-Host " [!!] $Msg" -ForegroundColor Yellow }
function Write-ERR { param([string]$Msg) Write-Host " [XX] $Msg" -ForegroundColor Red }
function Write-INFO { param([string]$Msg) Write-Host " [..] $Msg" -ForegroundColor Gray }
# ─────────────────────────────────────────────────────────────────────────────
Write-Section "1 / 5 — NETZWERK & ERREICHBARKEIT"
# ─────────────────────────────────────────────────────────────────────────────
Write-INFO "Ping $ComputerName ..."
$ping = Test-Connection -ComputerName $ComputerName -Count 2 -Quiet -ErrorAction SilentlyContinue
if ($ping) {
Write-OK "Ping erfolgreich"
} else {
Write-ERR "Ping fehlgeschlagen — Rechner nicht erreichbar oder ICMP geblockt"
}
Write-INFO "WinRM-Port 5985 (HTTP) ..."
$winrm = Test-NetConnection -ComputerName $ComputerName -Port 5985 -WarningAction SilentlyContinue -ErrorAction SilentlyContinue
if ($winrm.TcpTestSucceeded) {
Write-OK "WinRM Port 5985 offen"
} else {
Write-WARN "WinRM Port 5985 nicht erreichbar — Invoke-Command wird fehlschlagen (lokal kein Problem)"
}
# ─────────────────────────────────────────────────────────────────────────────
Write-Section "2 / 5 — INVOKE-COMMAND VERBINDUNGSTEST"
# ─────────────────────────────────────────────────────────────────────────────
$invokeParams = @{
ComputerName = $ComputerName
ScriptBlock = { $env:COMPUTERNAME }
ErrorAction = 'Stop'
}
if ($Credential) { $invokeParams['Credential'] = $Credential }
try {
$remoteHostname = Invoke-Command @invokeParams
Write-OK "Invoke-Command erfolgreich. Remote-Hostname: $remoteHostname"
} catch {
Write-ERR "Invoke-Command fehlgeschlagen: $_"
Write-WARN "Weitere Tests laufen lokal weiter — ggf. Ergebnisse nicht repräsentativ"
}
# ─────────────────────────────────────────────────────────────────────────────
Write-Section "3 / 5 — AUDIT-RICHTLINIEN (remote)"
# ─────────────────────────────────────────────────────────────────────────────
# Liefert nur Ergebnisse wenn das Remote-Konto Admin-Rechte auf dem Zielrechner hat.
$auditScript = {
# GUIDs sind sprach-unabhängig — funktioniert auf DE und EN Windows
$subcategories = @(
@{ Name = 'Logon'; GUID = '{0CCE9215-69AE-11D9-BED3-505054503030}' }
@{ Name = 'Logoff'; GUID = '{0CCE9216-69AE-11D9-BED3-505054503030}' }
@{ Name = 'Other Logon/Logoff Events'; GUID = '{0CCE921C-69AE-11D9-BED3-505054503030}' }
@{ Name = 'Special Logon'; GUID = '{0CCE921B-69AE-11D9-BED3-505054503030}' }
)
$results = foreach ($sub in $subcategories) {
$raw = (auditpol /get /subcategory:"$($sub.GUID)" 2>$null) -join ' '
$setting = if ($raw -match 'No Auditing|Keine') { 'Keine Überwachung ← PROBLEM' }
elseif ($raw -match 'Success and Failure|Erfolg und') { 'Erfolg und Fehler (optimal)' }
elseif ($raw -match 'Success|Erfolg') { 'Nur Erfolg' }
elseif ($raw -match 'Failure|Fehler') { 'Nur Fehler' }
else { "Unbekannt (raw: $($raw.Substring(0,[Math]::Min(80,$raw.Length))))" }
[PSCustomObject]@{ Unterkategorie = $sub.Name; Einstellung = $setting }
}
$results
}
$auditInvokeParams = @{
ComputerName = $ComputerName
ScriptBlock = $auditScript
ErrorAction = 'SilentlyContinue'
}
if ($Credential) { $auditInvokeParams['Credential'] = $Credential }
try {
$auditResults = Invoke-Command @auditInvokeParams
if ($auditResults) {
$auditResults | ForEach-Object {
$color = if ($_.Einstellung -match 'PROBLEM') { 'Red' }
elseif ($_.Einstellung -match 'optimal') { 'Green' }
else { 'Yellow' }
Write-Host (" {0,-40} {1}" -f $_.Unterkategorie, $_.Einstellung) -ForegroundColor $color
}
Write-Host ""
Write-WARN "4800/4801 (Lock/Unlock) und 4802/4803 (Screensaver) benötigen:"
Write-INFO " 'Other Logon/Logoff Events' = Erfolg und Fehler"
Write-WARN "4625 (fehlgesch. Anmeldung) benötigt:"
Write-INFO " 'Logon' = Erfolg und Fehler (nicht nur Erfolg!)"
} else {
Write-WARN "Keine Audit-Daten empfangen (evtl. fehlende Rechte oder auditpol nicht verfügbar)"
}
} catch {
Write-WARN "Audit-Abfrage fehlgeschlagen: $_"
}
# ─────────────────────────────────────────────────────────────────────────────
Write-Section "4 / 5 — EVENT-COUNTS PRO ID (letzte $TageRueckwaerts Tage)"
# ─────────────────────────────────────────────────────────────────────────────
$countScript = {
param([int]$Tage)
$seit = (Get-Date).AddDays(-$Tage)
$systemIDs = @(12, 13, 41, 42, 107, 1001, 1074, 1076, 6005, 6006, 6008, 6009, 6013)
$securityIDs = @(4624, 4625, 4634, 4647, 4648, 4672, 4778, 4779, 4800, 4801, 4802, 4803)
$counts = [System.Collections.Generic.List[PSCustomObject]]::new()
# System Log
foreach ($id in $systemIDs) {
$n = 0
try {
$evts = Get-WinEvent -FilterHashtable @{ LogName='System'; Id=$id; StartTime=$seit } -ErrorAction SilentlyContinue
$n = @($evts).Count
} catch {}
$counts.Add([PSCustomObject]@{
Log = 'System'
EventID = $id
Anzahl = $n
Status = if ($n -gt 0) { 'Daten vorhanden' } else { 'Keine Daten' }
})
}
# Power-Troubleshooter (System Log, spezieller Provider)
$n = 0
try {
$evts = Get-WinEvent -FilterHashtable @{
LogName='System'; ProviderName='Microsoft-Windows-Power-Troubleshooter'; Id=1; StartTime=$seit
} -ErrorAction SilentlyContinue
$n = @($evts).Count
} catch {}
$counts.Add([PSCustomObject]@{
Log = 'System (Power-Troubleshooter)'
EventID = 1
Anzahl = $n
Status = if ($n -gt 0) { 'Daten vorhanden' } else { 'Keine Daten' }
})
# Security Log
foreach ($id in $securityIDs) {
$n = 0
try {
$evts = Get-WinEvent -FilterHashtable @{ LogName='Security'; Id=$id; StartTime=$seit } -ErrorAction SilentlyContinue
$n = @($evts).Count
} catch {}
$counts.Add([PSCustomObject]@{
Log = 'Security'
EventID = $id
Anzahl = $n
Status = if ($n -gt 0) { 'Daten vorhanden' } else { 'Keine Daten' }
})
}
return $counts
}
$countInvokeParams = @{
ComputerName = $ComputerName
ScriptBlock = $countScript
ArgumentList = @($TageRueckwaerts)
ErrorAction = 'SilentlyContinue'
}
if ($Credential) { $countInvokeParams['Credential'] = $Credential }
try {
$countResults = Invoke-Command @countInvokeParams
if ($countResults) {
$countResults | ForEach-Object {
$color = if ($_.Anzahl -gt 0) { 'Green' } else { 'DarkGray' }
Write-Host (" {0,-35} ID {1,-6} {2,6} Einträge {3}" -f $_.Log, $_.EventID, $_.Anzahl, $_.Status) -ForegroundColor $color
}
}
} catch {
Write-WARN "Count-Abfrage fehlgeschlagen: $_"
}
# ─────────────────────────────────────────────────────────────────────────────
Write-Section "5 / 5 — ROHE PROPERTIES[] JE EVENT-ID (erster Treffer)"
# ─────────────────────────────────────────────────────────────────────────────
Write-INFO "Dies zeigt die tatsächlichen Properties-Indices — damit können"
Write-INFO "die Hardcode-Indices im Haupt-Script verifiziert werden."
Write-Host $sep2 -ForegroundColor DarkGray
$rawScript = {
param([int]$Tage, [bool]$Alle)
$seit = (Get-Date).AddDays(-$Tage)
$queries = @(
@{ Log = 'System'; IDs = @(12, 13, 41, 42, 107, 1001, 1074, 1076, 6005, 6006, 6008, 6009, 6013) }
@{ Log = 'Security'; IDs = @(4624, 4625, 4634, 4647, 4648, 4672, 4778, 4779, 4800, 4801, 4802, 4803) }
)
$output = [System.Collections.Generic.List[PSCustomObject]]::new()
foreach ($query in $queries) {
foreach ($id in $query.IDs) {
try {
$evts = Get-WinEvent -FilterHashtable @{
LogName = $query.Log
Id = $id
StartTime = $seit
} -ErrorAction SilentlyContinue
$subset = if ($Alle) { $evts } else { @($evts)[0] }
foreach ($ev in $subset) {
if ($null -eq $ev) { continue }
$propsFormatted = for ($i = 0; $i -lt $ev.Properties.Count; $i++) {
"[$i] = $($ev.Properties[$i].Value)"
}
$output.Add([PSCustomObject]@{
Log = $query.Log
EventID = $ev.Id
Zeitpunkt = $ev.TimeCreated
Provider = $ev.ProviderName
Properties = ($propsFormatted -join ' | ')
Message1stLine = ($ev.Message -split "`n")[0].Trim()
})
}
} catch {}
}
}
# Power-Troubleshooter separat
try {
$evts = Get-WinEvent -FilterHashtable @{
LogName = 'System'
ProviderName = 'Microsoft-Windows-Power-Troubleshooter'
Id = 1
StartTime = $seit
} -ErrorAction SilentlyContinue
$subset = if ($Alle) { $evts } else { @($evts)[0] }
foreach ($ev in $subset) {
if ($null -eq $ev) { continue }
$propsFormatted = for ($i = 0; $i -lt $ev.Properties.Count; $i++) {
"[$i] = $($ev.Properties[$i].Value)"
}
$output.Add([PSCustomObject]@{
Log = 'System (Power-Troubleshooter)'
EventID = $ev.Id
Zeitpunkt = $ev.TimeCreated
Provider = $ev.ProviderName
Properties = ($propsFormatted -join ' | ')
Message1stLine = ($ev.Message -split "`n")[0].Trim()
})
}
} catch {}
return $output | Sort-Object Log, EventID
}
$rawInvokeParams = @{
ComputerName = $ComputerName
ScriptBlock = $rawScript
ArgumentList = @($TageRueckwaerts, ([bool]$AllePropertiesAusgeben))
ErrorAction = 'SilentlyContinue'
}
if ($Credential) { $rawInvokeParams['Credential'] = $Credential }
try {
$rawResults = Invoke-Command @rawInvokeParams
if ($rawResults) {
foreach ($r in $rawResults) {
Write-Host $sep2 -ForegroundColor DarkGray
Write-Host (" [{0}] EventID {1} {2} Provider: {3}" -f $r.Log, $r.EventID, $r.Zeitpunkt, $r.Provider) -ForegroundColor White
Write-Host (" Meldung (1. Zeile): {0}" -f $r.Message1stLine) -ForegroundColor DarkYellow
Write-Host " Properties:" -ForegroundColor Cyan
$r.Properties -split ' \| ' | ForEach-Object {
Write-Host " $_" -ForegroundColor Gray
}
}
Write-Host $sep2 -ForegroundColor DarkGray
} else {
Write-WARN "Keine rohen Properties empfangen (keine Events im Zeitraum oder Verbindungsfehler)"
}
} catch {
Write-WARN "Raw-Properties-Abfrage fehlgeschlagen: $_"
}
Write-Host ""
Write-Section "ZUSAMMENFASSUNG"
Write-INFO "Bitte den vollständigen Output an Claude schicken."
Write-INFO "Besonders wichtig: Abschnitt 4 (Counts) und 5 (Properties)."
Write-INFO "Daraus kann das Haupt-Script gezielt korrigiert werden."
Write-Host $SEP -ForegroundColor DarkCyan