Registri eventi: Get-WinEvent
Get-WinEvent legge i registri eventi di Windows (Applicazione, Sistema, Sicurezza e quelli specifici dei servizi). Sostituisce il vecchio Get-EventLog.
Elencare i registri
Get-WinEvent -ListLog *
Get-WinEvent -ListLog * | Where-Object RecordCount -gt 0 | Sort-Object RecordCount -Descending | Select-Object -First 10 LogName, RecordCount
Get-WinEvent -ListLog Application, System, Security
Leggere gli eventi
Get-WinEvent -LogName System -MaxEvents 20
Get-WinEvent -LogName Application -MaxEvents 50 | Format-Table TimeCreated, Id, LevelDisplayName, Message -AutoSize
Get-WinEvent -LogName System -Oldest -MaxEvents 5
Il registro Security richiede una console amministratore.
Filtrare con FilterHashtable
Il metodo corretto e veloce: il filtro viene applicato dal sistema, non da PowerShell.
Get-WinEvent -FilterHashtable @{LogName='System'; Level=2}
Get-WinEvent -FilterHashtable @{LogName='System'; Level=1,2,3; StartTime=(Get-Date).AddHours(-24)}
Get-WinEvent -FilterHashtable @{LogName='Application'; ProviderName='Application Error'}
Get-WinEvent -FilterHashtable @{LogName='Security'; Id=4625; StartTime=(Get-Date).AddDays(-1)}
Get-WinEvent -FilterHashtable @{LogName='System'; Id=6005,6006,6008,41}
| Chiave | Significato |
|---|---|
LogName | Nome del registro |
ProviderName | Origine dell'evento |
Id | Identificativo evento (singolo o elenco) |
Level | Gravità: 1 Critico, 2 Errore, 3 Avviso, 4 Informazioni, 5 Dettagliato |
StartTime / EndTime | Intervallo di tempo |
Keywords | Parole chiave (es. 4503599627370496 per audit fallito) |
Non usare Where-Object su registri grandi quando puoi usare -FilterHashtable: la differenza è di ordini di grandezza.
Eventi utili da conoscere
| ID | Registro | Significato |
|---|---|---|
| 4624 | Security | Accesso riuscito |
| 4625 | Security | Accesso fallito |
| 4634 / 4647 | Security | Disconnessione |
| 4672 | Security | Accesso con privilegi speciali |
| 4720 | Security | Account utente creato |
| 4726 | Security | Account utente eliminato |
| 4740 | Security | Account bloccato |
| 6005 / 6006 | System | Avvio / arresto del servizio registro eventi |
| 6008 | System | Arresto imprevisto |
| 41 | System | Kernel-Power: riavvio senza arresto corretto |
| 1074 | System | Arresto o riavvio avviato da un utente o processo |
| 7034 / 7036 | System | Servizio terminato inaspettatamente / cambio stato |
| 1000 | Application | Arresto anomalo di un'applicazione |
Esempi pratici
Tentativi di accesso falliti nelle ultime 24 ore:
Get-WinEvent -FilterHashtable @{LogName='Security'; Id=4625; StartTime=(Get-Date).AddDays(-1)} |
ForEach-Object {
[pscustomobject]@{
Ora = $_.TimeCreated
Utente = $_.Properties[5].Value
IP = $_.Properties[19].Value
}
}
Chi ha riavviato il computer:
Get-WinEvent -FilterHashtable @{LogName='System'; Id=1074} -MaxEvents 5 | Select-Object TimeCreated, Message
Ultimo avvio del sistema:
(Get-CimInstance Win32_OperatingSystem).LastBootUpTime
Cercare un testo nel messaggio
Get-WinEvent -LogName System -MaxEvents 1000 | Where-Object Message -like "*disk*"
Esportare
Get-WinEvent -LogName System -MaxEvents 100 | Select-Object TimeCreated, Id, LevelDisplayName, Message | Export-Csv .\eventi.csv -NoTypeInformation -Encoding UTF8
wevtutil epl System C:\Backup\system.evtx
Registri di altri computer
Get-WinEvent -ComputerName SERVER01 -LogName System -MaxEvents 20
Nota su Get-EventLog
Get-EventLog -LogName System -Newest 10 funziona solo in Windows PowerShell 5.1 e non legge i registri moderni. Usa Get-WinEvent.