修改日志写入方式,支持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){
# 写入到 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)
}
}