Get-EventLog

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

语法

Get-EventLog
   [-LogName] <String>
   [-ComputerName <String[]>]
   [-Newest <Int32>]
   [-After <DateTime>]
   [-Before <DateTime>]
   [-UserName <String[]>]
   [[-InstanceId] <Int64[]>]
   [-Index <Int32[]>]
   [-EntryType <String[]>]
   [-Source <String[]>]
   [-Message <String>]
   [-AsBaseObject]
   [<CommonParameters>]
Get-EventLog
   [-ComputerName <String[]>]
   [-List]
   [-AsString]
   [<CommonParameters>]

说明

Get-EventLog cmdlet 获取本地和远程计算机上的事件和事件日志。 默认情况下,Get-EventLog 从本地计算机获取日志。 若要从远程计算机获取日志,请使用 ComputerName 参数。

可以使用 Get-EventLog 参数和属性值搜索事件。 该 cmdlet 获取与指定属性值匹配的事件。

包含 EventLog 名词的 PowerShell cmdlet 仅适用于 Windows 经典事件日志,例如应用程序、系统或安全性。 若要获取在 Windows Vista 及更高版本中使用 Windows 事件日志技术的日志,请使用 Get-WinEvent

注意

Get-EventLog 使用已弃用的 Win32 API。 结果可能不准确。 请改用 Get-WinEvent cmdlet。

示例

示例 1:获取本地计算机上的事件日志

此示例显示本地计算机上可用的事件日志列表。 Log 列中的名称与 LogName 参数一起使用,以指定搜索事件的日志。

Get-EventLog -List

Max(K)   Retain   OverflowAction      Entries  Log
------   ------   --------------      -------  ---
15,168        0   OverwriteAsNeeded   20,792   Application
15,168        0   OverwriteAsNeeded   12,559   System
15,360        0   OverwriteAsNeeded   11,173   Windows PowerShell

Get-EventLog cmdlet 使用 List 参数显示可用的日志。

示例 2:从本地计算机上的事件日志获取最近的条目

此示例从系统事件日志中获取最近的条目。

Get-EventLog -LogName System -Newest 5

Index   Time          EntryType    Source              InstanceID   Message
-----   ----          ---------    ------              ----------   -------
13820   Jan 17 19:16  Error        DCOM                     10016   The description for Event...
13819   Jan 17 19:08  Error        DCOM                     10016   The description for Event...
13818   Jan 17 19:06  Information  Service Control...  1073748864   The start type of the Back...
13817   Jan 17 19:05  Error        DCOM                     10016   The description for Event...
13815   Jan 17 19:03  Information  Microsoft-Windows...        35   The time service is now sync...

Get-EventLog cmdlet 使用 LogName 参数来指定系统事件日志。 Newest 参数返回五个最近的事件。

示例 3:查找事件日志中特定数量条目的所有源

此示例演示如何查找系统事件日志中 1000 个最新条目中包含的所有源。

$Events = Get-EventLog -LogName System -Newest 1000
$Events | Group-Object -Property Source -NoElement | Sort-Object -Property Count -Descending

Count   Name
-----   ----
  110   DCOM
   65   Service Control Manager
   51   Microsoft-Windows-Kern...
   14   EventLog
   14   BTHUSB
   13   Win32k

Get-EventLog cmdlet 使用 LogName 参数指定系统日志。 Newest 参数选择 1000 个最近的事件。 事件对象存储在 $Events 变量中。 $Events 对象沿管道向下发送到 Group-Object cmdlet。 Group-Object 使用 Property 参数按源分组对象,并计算每个源的对象数。 NoElement 参数从输出中删除组成员。 Sort-Object cmdlet 使用 Property 参数按每个源名称的计数进行排序。 Descending 参数按从高到低的计数对列表进行排序。

示例 4:从特定事件日志获取错误事件

此示例从系统事件日志中获取错误事件。

Get-EventLog -LogName System -EntryType Error

Index Time          EntryType   Source  InstanceID Message
----- ----          ---------   ------  ---------- -------
13296 Jan 16 13:53  Error       DCOM    10016 The description for Event ID '10016' in Source...
13291 Jan 16 13:51  Error       DCOM    10016 The description for Event ID '10016' in Source...
13245 Jan 16 11:45  Error       DCOM    10016 The description for Event ID '10016' in Source...
13230 Jan 16 11:07  Error       DCOM    10016 The description for Event ID '10016' in Source...

Get-EventLog cmdlet 使用 LogName 参数指定系统日志。 EntryType 参数筛选事件以仅显示 Error 事件。

示例 5:使用 InstanceId 和 Source 值从事件日志中获取事件

此示例从特定 InstanceId 和 Source 的系统日志中获取事件。

Get-EventLog -LogName System -InstanceId 10016 -Source DCOM

Index Time          EntryType  Source  InstanceID  Message
----- ----          ---------  ------  ----------  -------
13245 Jan 16 11:45  Error      DCOM         10016  The description for Event ID '10016' in Source...
13230 Jan 16 11:07  Error      DCOM         10016  The description for Event ID '10016' in Source...
13219 Jan 16 10:00  Error      DCOM         10016  The description for Event ID '10016' in Source...

Get-EventLog cmdlet 使用 LogName 参数指定系统日志。 InstanceID 参数选择具有指定实例 ID 的事件。 Source 参数指定事件属性。

示例 6:从多台计算机获取事件

此命令从三台计算机上的系统事件日志中获取事件:Server01、Server02 和 Server03。

Get-EventLog -LogName System -ComputerName Server01, Server02, Server03

Get-EventLog cmdlet 使用 LogName 参数指定系统日志。 ComputerName 参数使用逗号分隔的字符串列出要从中获取事件日志的计算机。

示例 7:获取消息中包含特定单词的所有事件

此命令从系统事件日志获取事件消息中包含特定单词的所有事件。 指定的 Message 参数的值可能包含在消息的内容中,但不显示在 PowerShell 控制台上。

Get-EventLog -LogName System -Message *description*

Index Time          EntryType   Source       InstanceID   Message
----- ----          ---------   ------       ----------   -------
13821 Jan 17 19:17  Error       DCOM              10016   The description for Event ID '10016'...
13820 Jan 17 19:16  Error       DCOM              10016   The description for Event ID '10016'...
13819 Jan 17 19:08  Error       DCOM              10016   The description for Event ID '10016'...

Get-EventLog cmdlet 使用 LogName 参数来指定系统事件日志。 Message 参数指定要在每个事件的消息字段中搜索的单词。

示例 8:显示事件的属性值

此示例演示如何显示事件的所有属性和值。

$A = Get-EventLog -LogName System -Newest 1
$A | Select-Object -Property *

EventID            : 10016
MachineName        : localhost
Data               : {}
Index              : 13821
Category           : (0)
CategoryNumber     : 0
EntryType          : Error
Message            : The description for Event ID '10016' in Source 'DCOM'...
Source             : DCOM
ReplacementStrings : {Local,...}
InstanceId         : 10016
TimeGenerated      : 1/17/2019 19:17:23
TimeWritten        : 1/17/2019 19:17:23
UserName           : username
Site               :
Container          :

Get-EventLog cmdlet 使用 LogName 参数来指定系统事件日志。 Newest 参数选择最新的事件对象。 对象存储在 $A 变量中。 $A 变量中的对象沿着管道发送到 Select-Object cmdlet。 Select-ObjectProperty 参数与星号(*)一起使用来选择所有对象的属性。

示例 9:使用源和事件 ID 从事件日志中获取事件

此示例获取指定 Source 和 Event 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.
Outlook       63   1073741887   Outlook detected a change notification.
Outlook       63   1073741887   The Exchange web service request succeeded.

Get-EventLog cmdlet 使用 LogName 参数指定应用程序事件日志。 Source 参数指定应用程序名称 Outlook。 对象沿管道向下发送到 Where-Object cmdlet。 对于管道中的每个对象,Where-Object cmdlet 使用变量 $_.EventID 将 Event ID 属性与指定的值进行比较。 对象沿管道向下发送到 Select-Object cmdlet。 Select-Object 使用 Property 参数选择要在 PowerShell 控制台中显示的属性。

示例 10:按属性获取事件和分组

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

Get-EventLog cmdlet 使用 LogName 参数指定系统日志。 UserName 参数包括星号(*)通配符,用于指定用户名的一部分。 事件对象沿管道向下发送到 Group-Object cmdlet。 Group-Object 使用 Property 参数指定 UserName 属性用于对对象进行分组,并计算每个用户名的对象数。 NoElement 参数从输出中删除组成员。 对象沿管道向下发送到 Select-Object cmdlet。 Select-Object 使用 Property 参数选择要在 PowerShell 控制台中显示的属性。

示例 11:获取在特定日期和时间范围内发生的事件

此示例从系统事件日志中获取指定日期和时间范围的 Error 事件。 BeforeAfter 参数设置日期和时间范围,但从输出中排除。

$Begin = Get-Date -Date '1/17/2019 08:00:00'
$End = Get-Date -Date '1/17/2019 17:00:00'
Get-EventLog -LogName System -EntryType Error -After $Begin -Before $End

Index Time          EntryType   Source   InstanceID  Message
----- ----          ---------   ------   ----------  -------
13821 Jan 17 13:40  Error       DCOM          10016  The description for Event ID...
13820 Jan 17 13:11  Error       DCOM          10016  The description for Event ID...
...
12372 Jan 17 10:08  Error       DCOM          10016  The description for Event ID...
12371 Jan 17 09:04  Error       DCOM          10016  The description for Event ID...

Get-Date cmdlet 使用 Date 参数指定日期和时间。 DateTime 对象存储在 $Begin$End 变量中。 Get-EventLog cmdlet 使用 LogName 参数指定系统日志。 EntryType 参数指定 Error 事件类型。 日期和时间范围由 After 参数和 $Begin 变量以及 Before 参数和 $End 变量设置。

参数

-After

获取在指定日期和时间之后发生的事件。 After 参数日期和时间从输出中排除。 输入 DateTime 对象,例如 Get-Date cmdlet 返回的值。

Type:DateTime
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-AsBaseObject

指示此 cmdlet 为每个事件返回标准 System.Diagnostics.EventLogEntry 对象。 如果没有此参数, Get-EventLog 将返回具有其他 EventLogNameSourceInstanceId 属性的扩展 PSObject 对象。

若要查看此参数的效果,请通过管道将事件传递给 Get-Member cmdlet,并检查结果中的 TypeName 值。

Type:SwitchParameter
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-AsString

指示此 cmdlet 将输出作为字符串返回,而不是作为对象返回。

Type:SwitchParameter
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Before

获取在指定日期和时间之前发生的事件。 Before 参数日期和时间从输出中排除。 输入 DateTime 对象,例如 Get-Date cmdlet 返回的值。

Type:DateTime
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-ComputerName

此参数指定远程计算机的 NetBIOS 名称、Internet 协议 (IP) 地址或全限定域名 (FQDN)。

如果未指定 ComputerName 参数,Get-EventLog 默认为本地计算机。 该参数还接受以点 (.) 来指定本地计算机。

ComputerName 参数不依赖于 Windows PowerShell 远程处理。 即使计算机未配置为运行远程命令,也可以将 Get-EventLogComputerName参数配合使用。

Type:String[]
Aliases:Cn
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-EntryType

以字符串数组指定此 cmdlet 获取的事件的条目类型。

此参数的可接受值为:

  • 错误
  • Information
  • FailureAudit
  • SuccessAudit
  • 警告
Type:String[]
Aliases:ET
Accepted values:Error, Information, FailureAudit, SuccessAudit, Warning
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Index

指定要从事件日志中获取的索引值。 该参数接受以逗号分隔的值字符串。

Type:Int32[]
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-InstanceId

指定要从事件日志中获取的实例 ID。 该参数接受以逗号分隔的值字符串。

Type:Int64[]
Position:1
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-List

显示计算机上的事件日志列表。

Type:SwitchParameter
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-LogName

指定一个事件日志的名称。 若要查找日志名称,请使用 Get-EventLog -List。 允许使用通配符。 此参数是必需的。

Type:String
Aliases:LN
Position:0
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:True

-Message

指定事件消息中的字符串。 可以使用此参数来搜索包含特定单词或短语的消息。 允许使用通配符。

Type:String
Aliases:MSG
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:True

-Newest

从最新的事件开始,获取指定的事件数。 需要事件数,例如 -Newest 100。 指定返回的最大事件数。

Type:Int32
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Source

以字符串数组指定写入此 cmdlet 获取的日志的源。 允许使用通配符。

Type:String[]
Aliases:ABO
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:True

-UserName

以字符串数组指定与事件关联的用户名。 输入名称或名称模式,例如 User01User*Domain01\User*。 允许使用通配符。

Type:String[]
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:True

输入

None

不能通过管道将输入传递给 Get-EventLog

输出

System.Diagnostics.EventLogEntry. System.Diagnostics.EventLog. System.String

如果指定了 LogName 参数,则输出是 System.Diagnostics.EventLogEntry 对象的集合。

如果仅指定 List 参数,则输出是 System.Diagnostics.EventLog 对象的集合。

如果同时指定了 ListAsString 参数,则输出是 System.String 对象的集合。

备注

Windows 预安装环境(Windows PE)不支持 cmdlet Get-EventLogGet-WinEvent