From fe3279468d41ebf9c377a9db0d19f241bd18bbe5 Mon Sep 17 00:00:00 2001 From: chuzhongzai Date: Sun, 22 Dec 2024 18:36:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=97=A5=E5=BF=97=E5=86=99?= =?UTF-8?q?=E5=85=A5=E6=96=B9=E5=BC=8F=EF=BC=8C=E6=94=AF=E6=8C=81win10?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logger.ps1 | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) 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) } }