Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell - Tail Windows Event Log? Is it possible?

How can i use powershell to tail a specific windows event log? Is it possible?

like image 745
TheOneTeam Avatar asked Mar 07 '13 02:03

TheOneTeam


People also ask

Does PowerShell have a Tail command?

The Tail command is popular in the Unix language and it is used to retrieve the specific number of lines from the end of the document or the log files. PowerShell doesn't have the command with the same name but from the PowerShell v3. 0 onwards, PowerShell has added -Tail parameter in the Get-Content cmdlet.

Can you end Windows event log?

Open the Windows Event Viewer: press Windows R , type eventvwr. msc and press Enter . Scroll down to Application and Service Logs , Microsoft , Windows , WFP . Right-click on a log process and select Disable Log .

How do I view Windows event logs in PowerShell?

Viewing the Windows PowerShell Event Log To examine the events and their properties, use the Sort-Object cmdlet, the Group-Object cmdlet, and the cmdlets that contain the Format verb (the Format cmdlets). For more information, type "Get-Help Get-EventLog" and "Get-Help Get-WmiObject".

Does Windows have a Tail command?

An advanced tail -f command with GUI, MakeLogic Tail is the tail for Windows. It can be used to monitor the log files of various servers and comes with a variety of other intuitive and useful features.

How do you Tail a file in Windows PowerShell?

Open it with notepad $PROFILE. Then in the text document, create a new function: function Tail ($path) { Get-content -tail 15 -path $path -wait } This way you can access the function each time you start PowerShell. This should be the accepted answer.


2 Answers

I've done this on occasion:

$idx = (get-eventlog -LogName System -Newest 1).Index

while ($true)
{
  start-sleep -Seconds 1
  $idx2  = (Get-EventLog -LogName System -newest 1).index
  get-eventlog -logname system -newest ($idx2 - $idx) |  sort index
  $idx = $idx2
  }
like image 66
mjolinor Avatar answered Sep 28 '22 11:09

mjolinor


Per MSDN docs:

Get-WinEvent is designed to replace the Get-EventLog cmdlet on computers running Windows Vista and later versions of Windows. Get-EventLog gets events only in classic event logs. Get-EventLog is retained in Windows PowerShell for backward compatibility.

And spurred on by my own need to tail a non-classic event log (would that be an event log nouveau perchance?) here is the wonderfully concise code of @mjolinor repurposed to use Get-WinEvent:

Set-PSDebug -Strict
function Get-WinEventTail($LogName, $ShowExisting=10) {
    if ($ShowExisting -gt 0) {
        $data = Get-WinEvent -provider $LogName -max $ShowExisting
        $data | sort RecordId
        $idx = $data[0].RecordId
    }
    else {
        $idx = (Get-WinEvent -provider $LogName -max 1).RecordId
    }

    while ($true)
    {
        start-sleep -Seconds 1
        $idx2  = (Get-WinEvent -provider $LogName -max 1).RecordId
        if ($idx2 -gt $idx) {
            Get-WinEvent -provider $LogName -max ($idx2 - $idx) | sort RecordId
        }
        $idx = $idx2

        # Any key to terminate; does NOT work in PowerShell ISE!
        if ($Host.UI.RawUI.KeyAvailable) { return; }
    }
}

I added in a few bells and whistles for convenience:

  • By default it shows the last 10 lines of the log initially, then concatenates new entries as they occur--you can adjust that to any number via the ShowExisting parameter.
  • It sorts records with oldest first (contrary to Get-WinEvent's default) due to the natural order that tail requires.
  • You can press any key to terminate (but not in PowerShellISE).
like image 30
Michael Sorens Avatar answered Sep 28 '22 09:09

Michael Sorens