How to stream windows event log on the command line?

I am looking to do the equivalent of:

tail -f

on Windows. I read this but looks like there is no way to stream the event log so it runs in an endless loop and displays events as they occur on the command-line. Is there any built-in command option/flag to do this in Windows?

1

The [System.Diagnostics.Eventing.Reader.EventLogWatcher]-based solution[*] in stackprotector’s answer is promising, but somewhat cumbersome due to requiring a temporary file that must be read separately.

This can be avoided with the following Trace-WinEvent function, which streams any newly added event records directly to the pipeline:

function Trace-WinEvent {

  param(
    [Parameter(Mandatory)] [string] $LogName
  )

  $sourceId = 'TraceWinEvent' # self-chosen
  try {
    $query = [System.Diagnostics.Eventing.Reader.EventLogQuery]::new($LogName, 'LogName')
    $watcher = [System.Diagnostics.Eventing.Reader.EventLogWatcher]::new($query)
    $watcher.Enabled = $true
    Write-Verbose "Registering for EventLogWatcher events for the '$LogName' log..."
    Register-ObjectEvent -InputObject $watcher -SourceIdentifier $sourceId -EventName EventRecordWritten
    Write-Verbose 'Waiting for such events indefinitely; use Ctrl+C to stop...'
    while ($true) {
      $evt = Wait-Event -SourceIdentifier $sourceId
      $evt | Remove-Event
      # Extract the event record.
      $evtRec = $evt.SourceEventArgs.EventRecord
      # Add a .Message NoteProperty member and output the decorated record.
      $evtRec | Add-Member -PassThru Message $evtRec.FormatDescription()
    }
  }
  finally {
    Unregister-Event $sourceId
  }
}

Invoke it, e.g., as follows:

# Trace (monitor) the Application event log for newly created records 
# and also capture them in variable $records.
# -Verbose provides verbose feedback.
# Use Ctrl-C to stop.
Trace-WinEvent Application -OutVariable records -Verbose 

Sample output:

Note:

  • Streaming to the pipeline not only produces instant console output, but also allows programmatic processing of the records – which are [System.Diagnostics.Eventing.Reader.EventLogRecord] instances – in the pipeline.

  • To stop tracing (monitoring), press Ctrl+C.

  • The implementation is based on:

    • Calling Register-ObjectEvent without an -Action script block, which makes PowerShell queue the events for on-demand retrieval.

    • This on-demand retrieval is performed in an infinite loop that calls Wait-Event to retrieve pending events one at a time, in a blocking manner.

    • Since PowerShell’s for-display formatting of EventLogRecord instances relies on the presence of a .Message ETS property (of type NoteProperty, which Get-WinEvent automatically decorates its output objects with), the above code manually decorates each instance extracted from an event object that way, via the .FormatDescription() method, and outputs the decorated instance to the pipeline.


[*] The other solution there, the Get-WinEventTail function, is best avoided, because it unconditionally outputs the last N entries every time, which will produce many duplicates that are likely to drown out the information of interest.

0

The equivalent of tail -f is Get-Content -Wait. But that does not help, because you still need your events in a text file. Windows stores its events in .evtx files at C:WindowsSystem32winevtLogs and they are binary.

As a workaround, you can print the last x events periodically:

function Get-WinEventTail {

    param(
        $LogName,
        $MaxEvents = 20
    )

    while ($true) {
        Get-WinEvent -LogName $LogName -MaxEvents $MaxEvents | Select-Object TimeCreated, Message
        Start-Sleep -Seconds 1
    }
}

Get-WinEventTail -LogName System

A more sophisticated approach is to use an EventLogWatcher to write incoming events into a text file and tail that file then:

function Write-WinEventsToTempFile {

    param(
        $LogName
    )

    try {
        $logFile = New-TemporaryFile
        $query = [System.Diagnostics.Eventing.Reader.EventLogQuery]::new($LogName,[System.Diagnostics.Eventing.Reader.PathType]::LogName)
        $watcher = [System.Diagnostics.Eventing.Reader.EventLogWatcher]::new($query)
        $watcher.Enabled = $true
        $action = {
            $myEvent = Get-WinEvent -LogName $eventArgs.EventRecord.LogName | Where-Object {$_.RecordId -eq $eventArgs.EventRecord.RecordId}
            "[$($myEvent[0].TimeCreated)]: $(($myEvent[0].Message -replace 'rn', '') -replace 't|s{2,}', ' ')" | Out-File $event.MessageData -Append
        }
        $job = Register-ObjectEvent -InputObject $watcher -SourceIdentifier WriteWinEventsToTempFile -EventName 'EventRecordWritten' -MessageData $logFile -Action $action
        Write-Host "Use `"Get-Content '$($logFile.FullName)' -Wait`" in another PowerShell window to tail the log. Press Ctrl+C, if you are done..."
        Wait-Event -SourceIdentifier WriteWinEventsToTempFile
    } finally {
        Remove-Job $job -Force
        Remove-Item $logFile
    }
}

Write-WinEventsToTempFile -LogName System

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật