Puppeteers Blog

Powershelliä UNIX-ylläpitäjille, osa 5

May 14, 2015 

Windowsissa tekstimuotoisia lokitiedostoja UNIX:in tapaan ole, vaan niitä vastaa EventLog, jonka sisältöä voi tutkia kätevästi PowerShellillä. EventLogin lokit (Logs) voi listata seuraavasti:

PS C:> Get-EventLog -List 
 
 Max(K) Retain OverflowAction Entries Log 
 ------ ------ -------------- ------- --- 
 20 480 0 OverwriteAsNeeded 3 787 Application 
 20 480 0 OverwriteAsNeeded 0 HardwareEvents 
 512 7 OverwriteOlder 0 Internet Explorer 
 20 480 0 OverwriteAsNeeded 0 Key Management Service 
 20 480 0 OverwriteAsNeeded 2 606 Security 
 20 480 0 OverwriteAsNeeded 1 684 System 
 15 360 0 OverwriteAsNeeded 4 823 Windows PowerShell 

Halutun lokin (alla System) viimeisimmät kirjaukset (alla 10) saa näytettyä -Newest -vivulla:

PS C:> Get-EventLog System -Newest 10 
 
 Index Time EntryType Source InstanceID Message 
 ----- ---- --------- ------ ---------- ------- 
 1685 touko 14 1... Information Microsoft-Windows... 12 Prosessi C:WindowsSystem32svc... 
 1684 touko 14 1... Error NetJoin 4097 The machine windows81amd64 attem... 
 1683 touko 14 1... Error NetJoin 4097 The machine windows81amd64 attem... 
 1682 touko 14 1... Error NetJoin 4097 The machine windows81amd64 attem... 
 1681 touko 14 1... Error NetJoin 4097 The machine windows81amd64 attem... 
 1680 touko 14 1... Warning Microsoft-Windows... 219 The driver DriverWudfRd failed... 
 1679 touko 14 1... Information Microsoft-Windows... 10114 UMDF-ohjainliitin ei voinut käyn...
 1678 touko 14 1... Information Microsoft-Windows... 12 Prosessi C:WindowsSystem32svc...
 1677 touko 14 1... Error NetJoin 4097 The machine windows81amd64 attem...
 1676 touko 14 1... Warning Microsoft-Windows... 1014 Nimen win8.ipv6.microsoft.com. n...

Lokeista voidaan suodattaa näkyviin pelkästään halutun tyyppiset tapahtumat Where-Object CmdLetillä:

PS C:> Get-EventLog System -Newest 10|Where-Object { $_.Source -eq "NetJoin" }
 
 Index Time EntryType Source InstanceID Message
 ----- ---- --------- ------ ---------- -------
 1684 touko 14 1... Error NetJoin 4097 The machine windows81amd64 attem...
 1683 touko 14 1... Error NetJoin 4097 The machine windows81amd64 attem...
 1682 touko 14 1... Error NetJoin 4097 The machine windows81amd64 attem...
 1681 touko 14 1... Error NetJoin 4097 The machine windows81amd64 attem...
 1677 touko 14 1... Error NetJoin 4097 The machine windows81amd64 attem...

Lopuksi kiinnostavalta näyttävä lokikirjaus voidaan tulostaa järkevässä muodossa yhdistämällä Where-Object ja Format-List:

PS C:> Get-EventLog System|Where-Object { $_.Index -eq 1684 }|Format-List
 
 Index : 1684
 EntryType : Error
 InstanceId : 4097
 Message : The machine windows81amd64 attempted to join the domain SMB but failed. The error c
 ode was 1332.
 Category : (0)
 CategoryNumber : 0
 ReplacementStrings : {SMB, windows81amd64, 1332}
 Source : NetJoin
 TimeGenerated : 14.5.2015 16:11:37
 TimeWritten : 14.5.2015 16:11:37
 UserName : windows81amd64Heino

Tämän viimeisimmän komennon voisi toteuttaa korvata myös Get-WinEvent CmdLetillä, mutta senkin kanssa on käytettävä Where-Objectia, joten tehokkuus lienee samaa luokkaa kuin Get-EventLogia käytettäessä.

Powershelliä UNIX-ylläpitäjille -artikkelisarjan muut osat:

Samuli Seppänen
Samuli Seppänen
Author archive
menucross-circle