Gateway-keepaliveForPowershell/logger.ps1

35 lines
1.0 KiB
PowerShell

. $(-Join($(Get-Location).Path, '\config.ps1'))
function Write-Log {
param (
[Parameter(Mandatory)]
[string]$Message, # 日志内容
[switch]$ToEventLog # 是否输出到事件管理器
)
# 时间戳前缀
$timestamp = (Get-Date -Format "yyyy-MM-dd HH:mm:ss")
$logEntry = "[$timestamp] $Message"
# 输出到控制台
if ($Global:isDebugging) {
Write-Host $logEntry
}
if ($ToEventLog){
# 创建一个事件日志实例
$eventLog = New-Object System.Diagnostics.EventLog
# 指定事件源
$eventLog.Source = "GateWay-KeepAliveForPowershell"
# 如果事件源不存在,需要先创建
if (-not [System.Diagnostics.EventLog]::SourceExists("GateWay-KeepAliveForPowershell")) {
[System.Diagnostics.EventLog]::CreateEventSource("GateWay-KeepAliveForPowershell", "Application")
}
# 写入事件日志
$eventLog.WriteEntry($Message, [System.Diagnostics.EventLogEntryType]::Information, 1000)
}
}