新事件日志链接到应用程序事件日志

时间:2015-03-04 22:13:29

标签: powershell logging

我正在使用Powershell v3.0

我正在尝试创建新的事件日志。 当我创建事件日志时,它似乎是应用程序日志的副本。

  • 新日志中包含所有应用程序日志消息,

  • 写入应用程序日志的新事件同时显示在

  • 写入新日志的新事件将同时显示在

  • 如果删除新事件日志,则应用程序日志内容为 删除(0条记录)。

以下是创建新事件日志的代码:

#Add Event Log if not already added
$CheckEL = @(Get-EventLog -List | Where-Object {$_.Log -eq $EventLogName})

if($CheckEL.Count -eq 0){
    if ([System.Diagnostics.EventLog]::SourceExists($EventLogSource)){
        [System.Diagnostics.EventLog]::DeleteEventSource($EventLogSource)
    }
    New-EventLog -LogName $EventLogName -Source $EventLogSource -ErrorAction Stop
    Limit-EventLog -LogName $EventLogName -OverflowAction OverWriteAsNeeded -MaximumSize 64KB
}

1 个答案:

答案 0 :(得分:0)

如果我这样做的话:

$EventLogName = "OpsBrain"; 
$EventLogSource="OpsBrainService" 

#Test if Event Log not already exists
$CheckEL = @(Get-EventLog -List | Where-Object {$_.Log -eq $EventLogName})

if($CheckEL.Count -eq 0){
    #Event Log does not yet exists, create it:        
    New-EventLog -LogName $EventLogName -Source $EventLogSource
}

我在事件日志中写了一些东西(否则你无法检索日志文件的内容):

Write-EventLog OpsBrain -Source $EventLogSource -EventId 1 -Message "Test"

然后我只检索那个对象:

Get-EventLog OpsBrain

   Index Time          EntryType   Source                 InstanceID Message
   ----- ----          ---------   ------                 ---------- -------
       1 mrt 09 12:26  Information OpsBrainService                 1 Test