diff --git a/logger.ps1 b/logger.ps1 index ef52ca9..666e175 100644 --- a/logger.ps1 +++ b/logger.ps1 @@ -17,25 +17,18 @@ function Write-Log { } if ($ToEventLog){ - # 写入到 Windows 事件日志 - $eventID = 1000 + # 创建一个事件日志实例 + $eventLog = New-Object System.Diagnostics.EventLog - # 确保事件源存在 - $source = "GateWay-KeepAliveForPowershell" - if (-not (Get-EventLog -LogName Application -Source $source -ErrorAction SilentlyContinue)) { - try { - New-EventLog -LogName Application -Source $source -ErrorAction Stop - } catch { - Write-Host "Error creating event source: $_" -ForegroundColor Red - return - } + # 指定事件源 + $eventLog.Source = "GateWay-KeepAliveForPowershell" + + # 如果事件源不存在,需要先创建 + if (-not [System.Diagnostics.EventLog]::SourceExists("GateWay-KeepAliveForPowershell")) { + [System.Diagnostics.EventLog]::CreateEventSource("GateWay-KeepAliveForPowershell", "Application") } # 写入事件日志 - try { - Write-EventLog -LogName Application -Source $source -EventID $eventID -EntryType 'Info' -Message $Message - } catch { - Write-Host "Failed to write to event log: $_" -ForegroundColor Red - } + $eventLog.WriteEntry($Message, [System.Diagnostics.EventLogEntryType]::Information, 1000) } }