2013-03-07 12 views

risposta

6

Ho fatto questo in alcune occasioni:

$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 
    } 
+0

Scusa, il primo script che ho postato non era corretto. È possibile testarlo avviandolo e riavviando il servizio w32time alcune volte per vedere il risultato. – mjolinor

3

Per documentazione MSDN:

Get-WinEvent è progettato per sostituire il Get-EventLog cmdlet su computer che eseguono Windows Vista e versioni successive di Windows. Get-EventLog ottiene gli eventi solo nei registri eventi classici. Get-EventLog è mantenuto in Windows PowerShell per compatibilità con le versioni precedenti.

E stimolato dal mio bisogno di coda un non registro eventi -classic (si tratterebbe di un evento registro nouveau per caso?) Ecco il codice meravigliosamente conciso di @mjolinor riproposto da utilizzare 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; } 
    } 
} 

ho aggiunto un paio di campane e fischietti per comodità:

  • per impostazione predefinita mostra le ultime 10 linee di inizialmente il log, quindi concatena le nuove voci nel momento in cui si verificano - è possibile adattarle a qualsiasi numero tramite il parametro ShowExisting.
  • Ordina i record con il primo (contrariamente al valore predefinito di Get-WinEvent) a causa dell'ordine naturale richiesto dalla coda.
  • È possibile premere qualsiasi tasto per terminare (ma non in PowerShellISE).
0

Ho sistemato alcuni errori in una risposta di cui sopra ...

Set-PSDebug -Strict 
function Get-WinEventTail($LogName, $ShowExisting=50) { 
    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) { 
      $data = Get-WinEvent -provider $LogName -max ($idx2 - $idx + 1000) | where {$_.RecordId -gt $idx} | sort RecordId 
      $data 
      $idx = $data[-1].RecordId 
     } 

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

Questa dovrebbe essere una modifica suggerita o un commento, non una nuova risposta. Sarai in grado di fare queste cose quando guadagnerai più reputazione. – Chris