修改日志写入方式,支持win10

This commit is contained in:
chuzhongzai 2024-12-22 18:36:15 +08:00
parent 3f7602d51b
commit fe3279468d

View File

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