[TOC]

系统日志查看与管理

Get-EventLog 命令 - 获取本地计算机或远程计算机上的事件日志或事件日志列表中的事件。

描述: 默认情况下Get EventLog从本地计算机获取日志,它仅适用于Windows经典事件日志,如应用程序、系统或安全性。。

Tips: 在Windows Vista和更高版本的Windows中获取使用Windows事件日志技术的日志,请使用 “get WinEvent” cmdlet。因为Get EventLog使用的Win32 API已弃用。


基础语法:

1
Get-EventLog [-LogName] <System.String> [[-InstanceId] <System.Int64[]>] [-After <System.DateTime>] [-AsBaseObject] [-Before <System.DateTime>] [-ComputerName <System.String[]>] [-EntryType {Error | Information | FailureAudit | SuccessAudit | Warning}] [-Index <System.Int32[]>] [-Message <System.String>] [-Newest <System.Int32>] [-Source <System.String[]>] [-UserName <System.String[]>] [<CommonParameters>]


基础示例:

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
77
78
79
80
81
82
# - 1.查看本机的事件日志
Get-EventLog -List
# Max(K) Retain OverflowAction Entries Log
# ------ ------ -------------- ------- ---
# 20,480 0 OverwriteAsNeeded 12,738 Application
# 20,480 0 OverwriteAsNeeded 0 HardwareEvents
# 512 7 OverwriteOlder 0 Internet Explorer
# 20,480 0 OverwriteAsNeeded 0 Key Management Service
# 128 0 OverwriteAsNeeded 64 OAlerts
# 20,480 0 OverwriteAsNeeded 33,075 Security
# 20,480 0 OverwriteAsNeeded 47,497 System
# 15,360 0 OverwriteAsNeeded 10,113 Windows PowerShell

# - 2.从本地计算机上的系统事件日志获取最近的条目
Get-EventLog -LogName System -Newest 5
# Index Time EntryType Source InstanceID Message
# ----- ---- --------- ------ ---------- -------
# 47492 6月 21 20:53 Warning DCOM 10016 无法找到源“DCOM”中事件 ID“10016”的说明。

# - 3.在事件日志中查找特定数目项的所有源
$Events = Get-EventLog -LogName System -Newest 1000
$Events | Group-Object -Property Source -NoElement | Sort-Object -Property Count -Descending
# Count Name
# ----- ----
# 922 Service Control Manager
# 27 Microsoft-Windows-Kern...
# 21 DCOM
# 10 Microsoft-Windows-DNS-...
# 6 Microsoft-Windows-Time...

# - 4.从特定事件日志获取错误事件(Error)、失败事件(FailureAudit)、成功事件(SuccessAudit)、Information 、Warning
Get-EventLog -LogName Security -EntryType Error

# - 5.从事件日志中获取具有InstanceId和源值的事件、或采用通配符的方式
Get-EventLog -LogName System -InstanceId 10016 -Source DCOM
# Index Time EntryType Source InstanceID Message
# ----- ---- --------- ------ ---------- -------
# 47492 6月 21 20:53 Warning DCOM 10016 无法找到源“DCOM”中事件 ID“10016”的说明。 ...
# 47
Get-EventLog -LogName System -Message *description*

# - 6.显示事件的属性值以及按属性获取事件和分组
$A = Get-EventLog -LogName System -Newest 1
$A | Select-Object -Property *
# EventID : 7040
# MachineName : WeiyiGeek
# Data : {}
# Index : 47496
# Category : (0)
# CategoryNumber : 0
# EntryType : Information
# Message : Background Intelligent Transfer Service 服务的启动类型从 自动启动 更改为 按需启动。
# Source : Service Control Manager
# ReplacementStrings : {Background Intelligent Transfer Service, 自动启动, 按需启动, BITS}
# InstanceId : 1073748864
# TimeGenerated : 2021/6/21 21:23:39
# TimeWritten : 2021/6/21 21:23:39
# UserName : NT AUTHORITY\SYSTEM
# Site :

Get-EventLog -LogName System -UserName NT* | Group-Object -Property UserName -NoElement |
Select-Object -Property Count, Name
# Count Name
# ----- ----
# 6031 NT AUTHORITY\SYSTEM
# 42 NT AUTHORITY\LOCAL SERVICE
# 4 NT AUTHORITY\NETWORK SERVICE

# - 7.获取在特定日期和时间范围内发生的事件
$Begin = Get-Date -Date '6/17/2021 08:00:00'
$End = Get-Date -Date '6/18/2021 17:00:00'
Get-EventLog -LogName System -EntryType Error -After $Begin -Before $End
# Index Time EntryType Source InstanceID Message
# ----- ---- --------- ------ ---------- -------
# 46787 6月 18 09:21 Error DCOM 10001 无法找到源“DCOM”中事件 ID“10001”的说明。

# - 8.使用源和事件ID从事件日志获取事件
Get-EventLog -LogName Application -Source Outlook | Where-Object {$_.EventID -eq 63} |
Select-Object -Property Source, EventID, InstanceId, Message
# Source EventID InstanceId Message
# ------ ------- ---------- -------
# Outlook 63 1073741887 The Exchange web service request succeeded.

企业实例

1
2
3
4
# (1) 系统开关机时间 (日志来源:Winlogon)
Get-EventLog -LogName System -Source * | % {if($_.EventID -eq "7001" -or $_.EventID -eq "7002"){write-host "$($_.TimeGenerated.ToString('yyyy-MM-dd HH:mm:ss')) # $($_.Message)" }}
2021-06-21 20:24:57 # 客户体验改善计划的用户登录通知
2021-06-21 14:54:53 # 客户体验改善计划的用户注销通知

(Get-WinEvent -ListLog Security).ProviderNames

您可以使用PowerShell获取与日志关联的提供程序列表。下面是一个示例,显示与安全日志关联的提供程序。