-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew-EventLogSource.ps1
More file actions
174 lines (147 loc) · 5.73 KB
/
Copy pathNew-EventLogSource.ps1
File metadata and controls
174 lines (147 loc) · 5.73 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
#Requires -RunAsAdministrator
<#
.SYNOPSIS
Erstellt die Event Log-Quelle für ProcessMonitorService
.DESCRIPTION
Dieses Script erstellt die benötigte Event Log-Quelle "ProcessMonitorService"
im Application-Log und führt einen Test durch.
.EXAMPLE
.\New-EventLogSource.ps1
.NOTES
- Muss als Administrator ausgeführt werden
- Erstellt Event Source: ProcessMonitorService
- Ziel-Log: Application
#>
param(
[string]$SourceName = "ProcessMonitorService",
[string]$LogName = "Application"
)
# Farben für bessere Lesbarkeit
$ErrorColor = "Red"
$WarningColor = "Yellow"
$SuccessColor = "Green"
$InfoColor = "Cyan"
function Write-ColorOutput {
param(
[string]$Message,
[string]$Color = "White"
)
Write-Host $Message -ForegroundColor $Color
}
function Test-AdminRights {
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($currentUser)
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
function Test-EventSource {
param([string]$Source)
try {
return [System.Diagnostics.EventLog]::SourceExists($Source)
}
catch {
Write-ColorOutput "Fehler beim Prüfen der Event Source: $($_.Exception.Message)" $ErrorColor
return $false
}
}
function New-EventSource {
param(
[string]$Source,
[string]$Log
)
try {
[System.Diagnostics.EventLog]::CreateEventSource($Source, $Log)
Write-ColorOutput "Event Log-Quelle '$Source' erfolgreich erstellt!" $SuccessColor
return $true
}
catch {
Write-ColorOutput "Fehler beim Erstellen der Event Source: $($_.Exception.Message)" $ErrorColor
return $false
}
}
function Write-TestEvent {
param(
[string]$Source,
[string]$Log
)
try {
$eventLog = New-Object System.Diagnostics.EventLog($Log)
$eventLog.Source = $Source
$message = "ProcessMonitorService Event Log-Quelle wurde erfolgreich eingerichtet. Setup durchgeführt am $(Get-Date -Format 'dd.MM.yyyy HH:mm:ss')"
$eventLog.WriteEntry($message, [System.Diagnostics.EventLogEntryType]::Information, 1000)
Write-ColorOutput "Test-Eintrag im Event Log erstellt (Event ID: 1000)" $SuccessColor
return $true
}
catch {
Write-ColorOutput "Fehler beim Schreiben des Test-Eintrags: $($_.Exception.Message)" $ErrorColor
return $false
}
finally {
if ($eventLog) {
$eventLog.Dispose()
}
}
}
function Show-EventLogInfo {
param([string]$Source)
Write-ColorOutput "`n=== Event Log Informationen ===" $InfoColor
Write-ColorOutput "Event Source: $Source" $InfoColor
Write-ColorOutput "Ziel-Log: Application" $InfoColor
Write-ColorOutput "`nSo öffnen Sie den Event Viewer:" $InfoColor
Write-ColorOutput "1. Windows + R drücken" $InfoColor
Write-ColorOutput "2. 'eventvwr.msc' eingeben und Enter drücken" $InfoColor
Write-ColorOutput "3. Navigieren zu: Windows-Protokolle > Anwendung" $InfoColor
Write-ColorOutput "4. Nach Quelle '$Source' filtern" $InfoColor
}
# Hauptprogramm
Clear-Host
Write-ColorOutput "=============================================" $InfoColor
Write-ColorOutput "ProcessMonitorService Event Log Setup" $InfoColor
Write-ColorOutput "=============================================" $InfoColor
Write-ColorOutput ""
# Administrator-Rechte prüfen
if (-not (Test-AdminRights)) {
Write-ColorOutput "FEHLER: Dieses Script muss als Administrator ausgeführt werden!" $ErrorColor
Write-ColorOutput ""
Write-ColorOutput "So führen Sie das Script als Administrator aus:" $WarningColor
Write-ColorOutput "1. PowerShell als Administrator öffnen" $WarningColor
Write-ColorOutput "2. Zu diesem Verzeichnis navigieren" $WarningColor
Write-ColorOutput "3. .\New-EventLogSource.ps1 ausführen" $WarningColor
Write-ColorOutput ""
Write-ColorOutput "Oder: Rechtsklick auf das Script > 'Mit PowerShell als Administrator ausführen'" $WarningColor
Write-ColorOutput ""
Read-Host "Drücken Sie Enter zum Beenden"
exit 1
}
Write-ColorOutput "✓ Administrator-Rechte bestätigt" $SuccessColor
Write-ColorOutput ""
# Event Source prüfen
Write-ColorOutput "Prüfe Event Log-Quelle '$SourceName'..." $InfoColor
if (Test-EventSource -Source $SourceName) {
Write-ColorOutput "Event Log-Quelle '$SourceName' existiert bereits." $WarningColor
$choice = Read-Host "Möchten Sie trotzdem einen Test-Eintrag erstellen? (j/N)"
if ($choice -match '^[jJyY]') {
Write-ColorOutput "`nErstelle Test-Eintrag..." $InfoColor
if (Write-TestEvent -Source $SourceName -Log $LogName) {
Show-EventLogInfo -Source $SourceName
}
}
} else {
Write-ColorOutput "Event Log-Quelle '$SourceName' existiert nicht. Wird erstellt..." $InfoColor
if (New-EventSource -Source $SourceName -Log $LogName) {
# Kurz warten, damit die Quelle verfügbar wird
Write-ColorOutput "Warte 2 Sekunden..." $InfoColor
Start-Sleep -Seconds 2
# Test-Eintrag erstellen
Write-ColorOutput "Erstelle Test-Eintrag..." $InfoColor
if (Write-TestEvent -Source $SourceName -Log $LogName) {
Show-EventLogInfo -Source $SourceName
}
}
}
Write-ColorOutput "`n=============================================" $SuccessColor
Write-ColorOutput "Setup abgeschlossen!" $SuccessColor
Write-ColorOutput "=============================================" $SuccessColor
Write-ColorOutput ""
Write-ColorOutput "Der ProcessMonitorService kann jetzt Event Log-Einträge schreiben." $InfoColor
Write-ColorOutput ""
Read-Host "Drücken Sie Enter zum Beenden"