PowerShell i 4 metody filtrowania dziennika zdarzeń

4 metody filtrowania dziennika zdarzeń

Warto wiedzieć

Generalnie w PowerShellu do wyświetlenia wpisów dziennika zdarzeń służą dwa cmdlety: Get-EventLog oraz Get-WinEvent. To drugie jest nieco nowsze i daje większe możliwości, jest również tym rekomendowanym rozwiązaniem, dlatego w moich przykładach Get-WinEvent gra główną rolę.

#Zapisanie do zmiennej przedziału czasowego (ostatnie 24h)
$last_24h = (Get-Date) - (New-TimeSpan -Day 1)

#Pobranie odpowiedniego dziennika zdarzeń i wyfiltrowanie wpisów, które są wcześniejsze niż 24h
Get-WinEvent -LogName 'Windows PowerShell' | Where-Object { $_.TimeCreated -ge $last_24h }



   ProviderName: PowerShell

TimeCreated                      Id LevelDisplayName Message                                                                                     
-----------                      -- ---------------- -------                                                                                     
23.03.2024 10:08:11             400 Informacje       Engine state is changed from None to Available. ...                                         
23.03.2024 10:08:11             600 Informacje       Provider "Variable" is Started. ...                                                         
23.03.2024 10:08:11             600 Informacje       Provider "Function" is Started. ...                                                         
23.03.2024 10:08:11             600 Informacje       Provider "FileSystem" is Started. ...                                                       
23.03.2024 10:08:11             600 Informacje       Provider "Environment" is Started. ...                                                      
23.03.2024 10:08:11             600 Informacje       Provider "Alias" is Started. ...                                                            
23.03.2024 10:08:11             600 Informacje       Provider "Registry" is Started. ...                                                         
22.03.2024 21:08:35             400 Informacje       Engine state is changed from None to Available. ...                                         
22.03.2024 21:08:35             600 Informacje       Provider "Variable" is Started. ...                                                         
22.03.2024 21:08:35             600 Informacje       Provider "Function" is Started. ...                                                         
22.03.2024 21:08:35             600 Informacje       Provider "FileSystem" is Started. ...                                                       
22.03.2024 21:08:35             600 Informacje       Provider "Environment" is Started. ...                                                      
22.03.2024 21:08:35             600 Informacje       Provider "Alias" is Started. ...                                                            
22.03.2024 21:08:35             600 Informacje       Provider "Registry" is Started. ...    
#Zapisanie do zmiennej przedziału czasowego (ostatnie 24h)
$last_24h = (Get-Date) - (New-TimeSpan -Day 1)

#Pobranie wpisów z dziennika zdarzeń Windows PowerShell zaczynając od daty 24h temu
Get-WinEvent -FilterHashtable @{LogName='Windows PowerShell'; StartTime=$last_24h}


   ProviderName: PowerShell

TimeCreated                      Id LevelDisplayName Message                                                                                     
-----------                      -- ---------------- -------                                                                                     
23.03.2024 10:08:11             400 Informacje       Engine state is changed from None to Available. ...                                         
23.03.2024 10:08:11             600 Informacje       Provider "Variable" is Started. ...                                                         
23.03.2024 10:08:11             600 Informacje       Provider "Function" is Started. ...                                                         
23.03.2024 10:08:11             600 Informacje       Provider "FileSystem" is Started. ...                                                       
23.03.2024 10:08:11             600 Informacje       Provider "Environment" is Started. ...                                                      
23.03.2024 10:08:11             600 Informacje       Provider "Alias" is Started. ...                                                            
23.03.2024 10:08:11             600 Informacje       Provider "Registry" is Started. ...                                                         
22.03.2024 21:08:35             400 Informacje       Engine state is changed from None to Available. ...                                         
22.03.2024 21:08:35             600 Informacje       Provider "Variable" is Started. ...                                                         
22.03.2024 21:08:35             600 Informacje       Provider "Function" is Started. ...                                                         
22.03.2024 21:08:35             600 Informacje       Provider "FileSystem" is Started. ...                                                       
22.03.2024 21:08:35             600 Informacje       Provider "Environment" is Started. ...                                                      
22.03.2024 21:08:35             600 Informacje       Provider "Alias" is Started. ...                                                            
22.03.2024 21:08:35             600 Informacje       Provider "Registry" is Started. ...              

Warto wiedzieć

Zapytania z wykorzystaniem tabeli skrótów mają następujące zasady:
– Wielkość liter w kluczach i wartościach nie ma znaczenia.
– W orzypadku kluczy LogName i ProviderName mają zastosowanie znaki wieloznaczne;
– Każdy klucz może być wymieniony tylko raz;
– Wartość Path przyjmuje ścieżki do plików z rozszerzeniem .etl, .evt i .evtx.
– Klucze LogName, Path i ProviderName mogą być używane w tym samym zapytaniu.
– Klucz UserID może przyjmować identyfikator zabezpieczeń (SID) lub nazwę konta domeny, która może być użyta do skonstruowania prawidłowego obiektu System.Security.Principal.NTAccount.
– Wartość Data przyjmuje dane zdarzenia w nienazwanym polu. Na przykład zdarzenia w klasycznych dziennikach zdarzeń.
– Klucz <named-data> reprezentuje nazwane pole danych zdarzenia.

Więcej na ten temat znajdziesz: https://learn.microsoft.com/pl-pl/powershell/scripting/samples/creating-get-winevent-queries-with-filterhashtable?view=powershell-7.4

# Użycie parametru -FilterXPath
$XPath = '*[System[TimeCreated[timediff(@SystemTime) <= 86400000]]]'
Get-WinEvent -LogName 'Windows PowerShell' -FilterXPath $XPath

   ProviderName: PowerShell

TimeCreated                      Id LevelDisplayName Message                                                                                     
-----------                      -- ---------------- -------                                                                                     
23.03.2024 10:08:11             400 Informacje       Engine state is changed from None to Available. ...                                         
23.03.2024 10:08:11             600 Informacje       Provider "Variable" is Started. ...                                                         
23.03.2024 10:08:11             600 Informacje       Provider "Function" is Started. ...                                                         
23.03.2024 10:08:11             600 Informacje       Provider "FileSystem" is Started. ...                                                       
23.03.2024 10:08:11             600 Informacje       Provider "Environment" is Started. ...                                                      
23.03.2024 10:08:11             600 Informacje       Provider "Alias" is Started. ...                                                            
23.03.2024 10:08:11             600 Informacje       Provider "Registry" is Started. ...                                                         
22.03.2024 21:08:35             400 Informacje       Engine state is changed from None to Available. ...                                         
22.03.2024 21:08:35             600 Informacje       Provider "Variable" is Started. ...                                                         
22.03.2024 21:08:35             600 Informacje       Provider "Function" is Started. ...                                                         
22.03.2024 21:08:35             600 Informacje       Provider "FileSystem" is Started. ...                                                       
22.03.2024 21:08:35             600 Informacje       Provider "Environment" is Started. ...                                                      
22.03.2024 21:08:35             600 Informacje       Provider "Alias" is Started. ...                                                            
22.03.2024 21:08:35             600 Informacje       Provider "Registry" is Started. ...                                                         
# Użycie parametru -FilterXML

$xmlQuery ="<QueryList>
  <Query Id='0' Path='Windows PowerShell'>
    <Select Path='Windows PowerShell'>*[System[TimeCreated[timediff(@SystemTime) &lt;= 86400000]]]</Select>
  </Query>
</QueryList>"

Get-WinEvent -FilterXML $xmlQuery

  ProviderName: PowerShell

TimeCreated                      Id LevelDisplayName Message                                                                                     
-----------                      -- ---------------- -------                                                                                     
23.03.2024 10:08:11             400 Informacje       Engine state is changed from None to Available. ...                                         
23.03.2024 10:08:11             600 Informacje       Provider "Variable" is Started. ...                                                         
23.03.2024 10:08:11             600 Informacje       Provider "Function" is Started. ...                                                         
23.03.2024 10:08:11             600 Informacje       Provider "FileSystem" is Started. ...                                                       
23.03.2024 10:08:11             600 Informacje       Provider "Environment" is Started. ...                                                      
23.03.2024 10:08:11             600 Informacje       Provider "Alias" is Started. ...                                                            
23.03.2024 10:08:11             600 Informacje       Provider "Registry" is Started. ...                                                         
22.03.2024 21:08:35             400 Informacje       Engine state is changed from None to Available. ...                                         
22.03.2024 21:08:35             600 Informacje       Provider "Variable" is Started. ...                                                         
22.03.2024 21:08:35             600 Informacje       Provider "Function" is Started. ...                                                         
22.03.2024 21:08:35             600 Informacje       Provider "FileSystem" is Started. ...                                                       
22.03.2024 21:08:35             600 Informacje       Provider "Environment" is Started. ...                                                      
22.03.2024 21:08:35             600 Informacje       Provider "Alias" is Started. ...                                                            
22.03.2024 21:08:35             600 Informacje       Provider "Registry" is Started. ...                                                         

Podsumowanie

Praca domowa


To wszystko na dziś!

Jeśli masz ciekawe spostrzeżenia lub doświadczenia w tym temacie – koniecznie podziel się nimi w komentarzach.
A jeśli moje materiały są dla Ciebie pomocne, możesz postawić mi wirtualną kawę.

Dzięki za wsparcie!

Postaw mi kawę na buycoffee.to

Adam Pietrzak

Trener IT | Autor szkoleń | Entuzjasta PowerShella

Administrator systemów i sieci wsparcia działań wojskowych z ponad 10-letnim doświadczeniem. Praktyk w dziedzinie bezpieczeństwa systemu Windows, automatyzacji zadań (PowerShell) oraz rozwiązań chmurowych. Trener i twórca materiałów edukacyjnych (szkolenia, warsztaty, artykuły, podręczniki). Pasjonat dzielenia się wiedzą i wspierania początkujących administratorów IT. Prywatnie – amator aktywnego wypoczynku i rodzinnych podróży.


PowerShell - 10 sposobów na optymalizację codziennej pracy
Podziel się

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *