fixed the event log error
This commit is contained in:
75
samy.ps1
75
samy.ps1
@@ -544,6 +544,77 @@ $ConfirmPreference = 'None'
|
|||||||
# - Level-based Event IDs and console colors
|
# - Level-based Event IDs and console colors
|
||||||
# - Global in-memory log cache
|
# - Global in-memory log cache
|
||||||
# - One-time Event Log/source initialization with optional auto-elevation
|
# - One-time Event Log/source initialization with optional auto-elevation
|
||||||
|
|
||||||
|
function Ensure-SVSMspEventLog {
|
||||||
|
[CmdletBinding()]
|
||||||
|
param(
|
||||||
|
[string]$EventSource = "SVSMSP_Module",
|
||||||
|
[string]$EventLog = "SVSMSP Events"
|
||||||
|
)
|
||||||
|
|
||||||
|
try {
|
||||||
|
# If the log already exists, we're done
|
||||||
|
if ([System.Diagnostics.EventLog]::Exists($EventLog)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
# If the source exists but is bound to another log, rebind it
|
||||||
|
if ([System.Diagnostics.EventLog]::SourceExists($EventSource)) {
|
||||||
|
$current = [System.Diagnostics.EventLog]::LogNameFromSourceName($EventSource, '.')
|
||||||
|
if ($current -ne $EventLog) {
|
||||||
|
[System.Diagnostics.EventLog]::DeleteEventSource($EventSource)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
New-EventLog -LogName $EventLog -Source $EventSource -ErrorAction Stop
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
Write-Host "[Warning] Failed to ensure Event Log '$EventLog': $($_.Exception.Message)" -ForegroundColor Yellow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Write-SvsMspEventLog {
|
||||||
|
[CmdletBinding()]
|
||||||
|
param(
|
||||||
|
[Parameter(Mandatory)]
|
||||||
|
[string]$Message,
|
||||||
|
|
||||||
|
[ValidateSet('Information','Warning','Error')]
|
||||||
|
[string]$EntryType = 'Information',
|
||||||
|
|
||||||
|
[int]$EventId = 1000,
|
||||||
|
|
||||||
|
[string]$EventSource = 'SVSMSP_Module',
|
||||||
|
[string]$EventLog = 'SVSMSP Events'
|
||||||
|
)
|
||||||
|
|
||||||
|
# First attempt
|
||||||
|
try {
|
||||||
|
Write-EventLog -LogName $EventLog -Source $EventSource -EntryType $EntryType -EventId $EventId -Message $Message -ErrorAction Stop
|
||||||
|
return
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
$errMsg = $_.Exception.Message
|
||||||
|
|
||||||
|
# Only self-heal for the specific missing-log case
|
||||||
|
if ($errMsg -like '*The Log name*SVSMSP Events*does not exist*') {
|
||||||
|
Ensure-SVSMspEventLog -EventSource $EventSource -EventLog $EventLog
|
||||||
|
|
||||||
|
# Retry once after creating the log
|
||||||
|
try {
|
||||||
|
Write-EventLog -LogName $EventLog -Source $EventSource -EntryType $EntryType -EventId $EventId -Message $Message -ErrorAction Stop
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
Write-Host "[Warning] Failed to write to '$EventLog' even after Ensure-SVSMspEventLog: $($_.Exception.Message)" -ForegroundColor Yellow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Write-Host "[Warning] Failed to write to '$EventLog': $errMsg" -ForegroundColor Yellow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function Write-LogHelper {
|
function Write-LogHelper {
|
||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
@@ -789,6 +860,10 @@ if (-not [System.Diagnostics.EventLog]::SourceExists('$EventSource')) {
|
|||||||
$invokeParams.PassThru = $true
|
$invokeParams.PassThru = $true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($LogToEvent.IsPresent) {
|
||||||
|
Ensure-SVSMspEventLog -EventSource $EventSource -EventLog $EventLog
|
||||||
|
}
|
||||||
|
|
||||||
if ($PSBoundParameters.ContainsKey('ForegroundColorOverride')) {
|
if ($PSBoundParameters.ContainsKey('ForegroundColorOverride')) {
|
||||||
# 1) print to console with the override color
|
# 1) print to console with the override color
|
||||||
Write-Host $formatted -ForegroundColor $ForegroundColorOverride
|
Write-Host $formatted -ForegroundColor $ForegroundColorOverride
|
||||||
|
|||||||
Reference in New Issue
Block a user