获取本地或远程计算机上的事件日志或事件日志列表中的事件。
语法
Get-EventLog [-AsString] [-ComputerName <string[]>] [-List] [<CommonParameters>] Get-EventLog [-LogName] <string> [[-InstanceId] <Int64[]>] [-After <DateTime>] [-AsBaseObject] [-Before <DateTime>] [-ComputerName <string[]>] [-EntryType <string[]>] [-Index <Int32[]>] [-Message <string>] [-Newest <int>] [-Source <string[]>] [-UserName <string[]>] [<CommonParameters>]
说明
Get-EventLog cmdlet 获取本地和远程计算机上的事件和事件日志。
使用 Get-EventLog 的参数来按事件属性值搜索事件。Get-EventLog 只获取与指定的所有属性值匹配的事件。
包含 EventLog 名词的 cmdlet (EventLog cmdlet) 仅适用于传统事件日志。要从使用 Windows Vista 以及 Windows 更高版本中的 Windows 事件日志技术的日志中获取事件,请使用 Get-WinEvent。
参数
-After <DateTime>
只获取在指定的日期和时间之后发生的事件。输入 DateTime 对象,例如 Get-Date cmdlet 返回的 DateTime 对象。
是否为必需? |
false |
位置? |
named |
默认值 |
|
是否接受管道输入? |
false |
是否接受通配符? |
false |
-AsBaseObject
为每个事件返回一个标准 System.Diagnostics.EventLogEntry 对象。如果不使用此参数,则 Get-EventLog 返回具有附加的 EventLogName、Source 和 InstanceId 属性的扩展 PSObject 对象。
要查看此参数的效果,请将事件通过管道传送给 Get-Member cmdlet 并检查结果中的 TypeName 值。
是否为必需? |
false |
位置? |
named |
默认值 |
False |
是否接受管道输入? |
false |
是否接受通配符? |
false |
-AsString
以字符串而非对象的形式返回输出。
是否为必需? |
false |
位置? |
named |
默认值 |
|
是否接受管道输入? |
false |
是否接受通配符? |
false |
-Before <DateTime>
只获取在指定的日期和时间之前发生的事件。输入 DateTime 对象,例如 Get-Date cmdlet 返回的 DateTime 对象。
是否为必需? |
false |
位置? |
named |
默认值 |
|
是否接受管道输入? |
false |
是否接受通配符? |
false |
-ComputerName <string[]>
指定远程计算机。默认值为本地计算机。
键入远程计算机的 NetBIOS 名称、Internet 协议 (IP) 地址或完全限定的域名。要指定本地计算机,请键入计算机名称、句点 (.) 或“localhost”。
此参数不依赖于 Windows PowerShell 远程处理。即使您的计算机未配置为运行远程命令,您也可以使用 Get-EventLog 的 ComputerName 参数。
是否为必需? |
false |
位置? |
named |
默认值 |
|
是否接受管道输入? |
false |
是否接受通配符? |
false |
-EntryType <string[]>
只获取具有指定条目类型的事件。有效值包括 Error、Information、FailureAudit、SuccessAudit 和 Warning。默认值为所有事件。
是否为必需? |
false |
位置? |
named |
默认值 |
All events |
是否接受管道输入? |
false |
是否接受通配符? |
false |
-Index <Int32[]>
只获取具有指定索引值的事件。
是否为必需? |
false |
位置? |
named |
默认值 |
All events |
是否接受管道输入? |
false |
是否接受通配符? |
false |
-InstanceId <Int64[]>
只获取具有指定实例 ID 的事件。
是否为必需? |
false |
位置? |
2 |
默认值 |
|
是否接受管道输入? |
false |
是否接受通配符? |
false |
-List
获取计算机上的事件日志列表。
是否为必需? |
false |
位置? |
named |
默认值 |
|
是否接受管道输入? |
false |
是否接受通配符? |
false |
-LogName <string>
指定事件日志。输入一个事件日志的日志名称(Log 属性的值;而非 LogDisplayName)。不允许使用通配符。此参数是必需的。
是否为必需? |
true |
位置? |
1 |
默认值 |
|
是否接受管道输入? |
false |
是否接受通配符? |
false |
-Message <string>
获取其消息中具有指定字符串的事件。可以使用此属性来搜索包含特定单词或短语的消息。允许使用通配符。
是否为必需? |
false |
位置? |
named |
默认值 |
|
是否接受管道输入? |
false |
是否接受通配符? |
true |
-Newest <int>
指定检索的事件的最大数目。Get-EventLog 从日志中的最新事件开始,获取指定数目的事件。
是否为必需? |
false |
位置? |
named |
默认值 |
|
是否接受管道输入? |
false |
是否接受通配符? |
false |
-Source <string[]>
获取由指定的源写入日志的事件。允许使用通配符。
是否为必需? |
false |
位置? |
named |
默认值 |
|
是否接受管道输入? |
false |
是否接受通配符? |
true |
-UserName <string[]>
只获取与指定的用户名相关联的事件。输入名称或名称模式,如 User01、User* 或 Domain01\User*。允许使用通配符。
是否为必需? |
false |
位置? |
named |
默认值 |
|
是否接受管道输入? |
false |
是否接受通配符? |
true |
<CommonParameters>
此 cmdlet 支持通用参数:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。有关详细信息,请参阅 about_Commonparameters.
输入和输出
输入类型是指可通过管道传递给 cmdlet 的对象的类型。返回类型是指 Cmdlet 所返回对象的类型。
输入 |
无。 不能通过管道将输入传递给此 cmdlet。 |
输出 |
System.Diagnostics.EventLogEntry.System.Diagnostics.EventLog.System.String 如果指定了 LogName 参数,则输出将为 EventLogEntry 对象 (System.Diagnostics.EventLogEntry) 的集合。 如果仅指定了 List 参数,则输出将为 EventLog 对象 (System.Diagnostics.EventLog) 的集合。 如果同时指定了 List 和 AsString 参数,则输出将为 String (System.String) 的集合。 |
示例 1
C:\PS>get-eventlog -list 说明 ----------- 此命令显示有关计算机上的事件日志的信息。
示例 2
C:\PS>get-eventlog -newest 5 -logname application 说明 ----------- 此命令显示 Application 事件日志中的五个最新条目。
示例 3
C:\PS>$events = get-eventlog -logname system -newest 1000 C:\PS> $events | group-object -property source -noelement | sort-object -property count -descending Count Name ----- ---- 75 Service Control Manager 12 Print 6 UmrdpService 2 DnsApi 2 DCOM 1 Dhcp 1 TermDD 1 volsnap 说明 ----------- 此示例演示如何查找系统事件日志中的 1000 个最新条目中表示的所有源。 第一个命令从系统事件日志中获取 1000 个最新条目,并将这些条目存储在 $events 变量中。 第二个命令使用管道运算符 (|) 将 $events 中的事件发送给 Group-Object cmdlet,后者按 Source 属性的值对条目进行分组。该命令使用第二个管道运算符将经过分组的事件发送给 Sort-Object cmdlet,后者对其进行降序排序,因此出现频率最高的源将列在最前面。 Source 就是事件日志条目的属性。要查看事件日志条目的所有属性,请将事件通过管道传送给 Get-Member cmdlet。
示例 4
C:\PS>get-eventlog -logname System -EntryType Error 说明 ----------- 此命令只获取系统事件日志中的错误事件。
示例 5
C:\PS>get-eventlog -logname System -instanceID 3221235481 -Source "DCOM" 说明 ----------- 此命令从系统日志中获取 InstanceID 为 3221235481 且 Source 值为“DCOM”的事件。
示例 6
C:\PS>get-eventlog -logname "Windows PowerShell" -computername localhost, Server01, Server02 说明 ----------- 此命令从三台计算机上的“Windows PowerShell”事件日志中获取事件,这三台计算机分别是 Server01、Server02 和本地计算机(称为“localhost”)。
示例 7
C:\PS>get-eventlog -logname "Windows PowerShell" -message "*failed*" 说明 ----------- 此命令获取 Windows PowerShell 事件日志中消息值包括单词“failed”的所有事件。
示例 8
C:\PS>$a = get-eventlog -log System -newest 1 C:\PS> $a | format-list -property * EventID : 7036 MachineName : Server01 Data : {} Index : 10238 Category : (0) CategoryNumber : 0 EntryType : Information Message : The description for Event ID Source : Service Control Manager ReplacementStrings : {WinHTTP Web Proxy Auto-Disco InstanceId : 1073748860 TimeGenerated : 4/11/2008 9:56:05 PM TimeWritten : 4/11/2008 9:56:05 PM UserName : Site : Container : 说明 ----------- 此示例演示如何显示事件的所有属性值。 第一个命令获取系统事件日志中的最新事件并将其保存在 $a 变量中。 第二个命令使用管道运算符 (|) 将 $a 中的事件发送给 Format-List 命令,后者将显示所有 (*) 事件属性。
示例 9
C:\PS>get-eventlog -log application -source outlook | where {$_.eventID -eq 34} 说明 ----------- 此命令获取应用程序事件日志中源为 Outlook 且事件 ID 为 34 的事件。即使 Get-EventLog 没有 EventID 参数,也可以使用 Where-Object cmdlet 来根据任何事件属性的值选择事件。
示例 10
C:\PS>get-eventlog -log system -username NT* | group-object -property username -noelement | format-table Count, Name -auto Count Name ----- ---- 6031 NT AUTHORITY\SYSTEM 42 NT AUTHORITY\LOCAL SERVICE 4 NT AUTHORITY\NETWORK SERVICE 说明 ----------- 此命令返回系统日志中按其 UserName 属性值分组的事件。Get-EventLog 命令使用 UserName 参数来只获取用户名以“NT*”开头的事件。
示例 11
C:\PS>$May31 = get-date 5/31/08 C:\PS> $July1 = get-date 7/01/08 C:\PS> get-eventlog -log "Windows PowerShell" -entrytype Error -after $may31 -before $july1 说明 ----------- 此命令获取 Windows Powershell 事件日志中 2008 年 6 月发生的所有错误。
另请参阅