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. ...                                                         

Warto wiedzieć

Jeżeli nigdy wcześniej nie konstruowałeś zapytań XML w nauce może pomóc Ci standardowy Dziennik Zdarzeń. Wystarczy, że przejdziesz do sekcji Jakiś tam widok, następnie wybierzesz swoje filtrowanie. W moim przypadku będą to wpisy z dziennika zdarzeń Windows PowerShell za ostatnie 24 h. Kiedy filtr gotowy przejdz do zakładki XML. Tam znajdziesz gotowe zapytanie. Tylko zwróć uwage na cudzysłowy, czasami PowerShell opacznie je interpretuje.

Praca domowa

Podsumowanie

Tyle na dziś. Jeżeli masz jakieś ciekawe informacje w tym temacie to podziel się w komentarzach.

Jeżeli uważasz, że dobrze wykonaliśmy swoją robotę i dowiedziałeś się czegoś nowego to postaw nam wirtualną kawę.

Postaw mi kawę na buycoffee.to

Darmowy Ebook
Podziel się

Dodaj komentarz

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