changed the write-loghelper

This commit is contained in:
2025-07-03 01:03:04 -04:00
parent f06e45ff3c
commit 372adaee1f

View File

@@ -331,32 +331,36 @@
} }
# ─── Event Log ────────────────────────────────────────────────── # ─── Event Log ──────────────────────────────────────────────────
if ($LogToEvent) { if ($LogToEvent) {
# 1) Ensure your custom source/log exist try {
if (-not [System.Diagnostics.EventLog]::SourceExists($EventSource)) { # 1) Ensure your custom source/log exist
New-EventLog -LogName $EventLog -Source $EventSource if (-not [System.Diagnostics.EventLog]::SourceExists($EventSource)) {
} New-EventLog -LogName $EventLog -Source $EventSource -ErrorAction Stop
}
} catch {
Write-Host "[Warning] Could not create event log '$EventLog' or source '$EventSource': $($_.Exception.Message)" -ForegroundColor Yellow
return
}
# 2) Map level to entry type # 2) Map level to entry type
$entryType = if ($Level -in 'Warning','Error') { $Level } else { 'Information' } $entryType = if ($Level -in 'Warning','Error') { $Level } else { 'Information' }
# 3) Write to the Windows event log # 3) Write to the Windows event log
try { try {
Write-EventLog ` Write-EventLog `
-LogName $EventLog ` -LogName $EventLog `
-Source $EventSource ` -Source $EventSource `
-EntryType $entryType ` -EntryType $entryType `
-EventID $EventID ` -EventID $EventID `
-Message $fmt -Message $fmt
} }
catch { catch {
Write-Host "[Warning] EventLog failed: $_" -ForegroundColor Yellow Write-Host "[Warning] EventLog failed: $($_.Exception.Message)" -ForegroundColor Yellow
} }
} }
# ─── PassThru ──────────────────────────────────────────────────── if ($PassThru) { return $Global:LogCache[-1] }
if ($PassThru) { return $Global:LogCache[-1] } }
}
# ───────────────────────────────────────────────────────────────────────── # ─────────────────────────────────────────────────────────────────────────
# WRITE-LOG HYBRID (single definition, chooses at runtime if we use the # WRITE-LOG HYBRID (single definition, chooses at runtime if we use the