-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathgather log events.ps1
78 lines (72 loc) · 4.47 KB
/
gather log events.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#This script is intended to pull logs from a local server.
#INSTEAD, consider "gather log events - remting.ps1" in the toolbox to pull these logs down using PowerShell Remoting.
#Must launch PowerShell as an Administrator to read from the Security log
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted -Force
<#
$NumDays = -30
$EventLog_Application = Get-EventLog -LogName "Application" -After (Get-Date).AddDays($NumDays) |
? { $_.entryType -Match "Error" -and "Critical" -and "Warning" } | Group-Object -Property EventID |
ForEach-Object { $_.Group[0] | Add-Member -PassThru -NotePropertyName Count -NotePropertyValue $_.Count | Add-Member -PassThru -NotePropertyName LogSource -NotePropertyValue "Application" } |
Sort-Object Count -Descending -Unique |
Select-Object LogSource, Count, @{name="Latest";expression={$_.TimeGenerated}}, EventID, Source, Message ;
$EventLog_System = Get-EventLog -LogName "System" -After (Get-Date).AddDays($NumDays) |
? { $_.entryType -Match "Error" -and "Critical" -and "Warning" } | Group-Object -Property EventID |
ForEach-Object { $_.Group[0] | Add-Member -PassThru -NotePropertyName Count -NotePropertyValue $_.Count | Add-Member -PassThru -NotePropertyName LogSource -NotePropertyValue "System" } |
Sort-Object Count -Descending -Unique |
Select-Object LogSource, Count, @{name="Latest";expression={$_.TimeGenerated}}, EventID, Source, Message ;
$EventLog_Security = Get-EventLog -LogName "Security" -After (Get-Date).AddDays($NumDays) |
? { $_.entryType -Match "Error" -and "Critical" } | Group-Object -Property EventID |
ForEach-Object { $_.Group[0] | Add-Member -PassThru -NotePropertyName Count -NotePropertyValue $_.Count | Add-Member -PassThru -NotePropertyName LogSource -NotePropertyValue "Security" } |
Sort-Object Count -Descending -Unique |
Select-Object LogSource, Count, @{name="Latest";expression={$_.TimeGenerated}}, EventID, Source, Message ;
@( $EventLog_System; $EventLog_Application; $EventLog_Security) | Out-GridView;
#>
## Version that works on PowerShell 2.0 because of Add-Member and Out-Gridview dependencies aren't supported until 3.0
clear-host
$numDays = -90
$timestamp = (Get-Date).ToString('yyyyMMddTHHmmss')
$exportpath = "C:\temp\"+$env:computername+" log export " +$timestamp+".csv"
$eventLog_Application = @()
Get-EventLog -LogName "Application" -After (Get-Date).AddDays($numDays) |
Where-Object {$_.EntryType -match "Error" -or "Critical" -or "Warning"} |
Group-Object -Property EventID | ForEach {
$currentGroup = $_.Group
$latestMessage = $currentGroup | Sort-Object -Property Time -Descending | Select-Object -First 1
$obj = "" | Select-Object -Property Count, LogSource, Latest, EventID, Source, Message
$obj.Count = $currentGroup.Count
$obj.LogSource = "Application"
$obj.Latest = $latestMessage.TimeGenerated
$obj.EventID = $latestMessage.EventID
$obj.Source = $latestMessage.Source
$obj.Message = $latestMessage.Message
$eventLog_Application += $obj
}
Get-EventLog -LogName "System" -After (Get-Date).AddDays($numDays) |
Where-Object {$_.EntryType -match "Error" -or "Critical" -or "Warning"} |
Group-Object -Property EventID | ForEach {
$currentGroup = $_.Group
$latestMessage = $currentGroup | Sort-Object -Property Time -Descending | Select-Object -First 1
$obj = "" | Select-Object -Property Count, LogSource, Latest, EventID, Source, Message
$obj.Count = $currentGroup.Count
$obj.LogSource = "System"
$obj.Latest = $latestMessage.TimeGenerated
$obj.EventID = $latestMessage.EventID
$obj.Source = $latestMessage.Source
$obj.Message = $latestMessage.Message
$eventLog_Application += $obj
}
Get-EventLog -LogName "Security" -After (Get-Date).AddDays($numDays) |
Where-Object {$_.EntryType -match "Failure*"} |
Group-Object -Property EventID | ForEach {
$currentGroup = $_.Group
$latestMessage = $currentGroup | Sort-Object -Property Time -Descending | Select-Object -First 1
$obj = "" | Select-Object -Property Count, LogSource, Latest, EventID, Source, Message
$obj.Count = $currentGroup.Count
$obj.LogSource = "Security"
$obj.Latest = $latestMessage.TimeGenerated
$obj.EventID = $latestMessage.EventID
$obj.Source = $latestMessage.Source
$obj.Message = $latestMessage.Message
$eventLog_Application += $obj
}
$eventLog_Application | Sort-Object -Property Count, Latest -Descending | Export-Csv -Path $exportpath -Encoding ascii -NoTypeInformation