在本機或遠端電腦上取得事件記錄中的事件或事件記錄檔的清單。

語法

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 傳回的項目。

必要?

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 傳回的項目。

必要?

false

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

-ComputerName <string[]>

指定遠端電腦。預設為本機電腦。

請輸入遠端電腦的 NetBIOS 名稱、網際網路通訊協定 (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[]>

只取得具有指定執行個體識別碼的事件。

必要?

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 參數,輸出會是字串 (System.String) 的集合。

範例 1

C:\PS>get-eventlog -list

描述
-----------
這個命令會顯示電腦上之事件記錄檔的相關資訊。






範例 2

C:\PS>get-eventlog -newest 5 -logname 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,該 Cmdlet 會依照 Source 屬性的值將記錄分成不同的群組。這個命令使用第二個管線運算子,將分組的事件傳送到 Sort-Object Cmdlet,這個 Cmdlet 會遞減順序排序事件,所以出現最頻繁的來源會列在最前面。

來源只是事件記錄項目的屬性。若要檢視事件記錄項目的屬性,請透過管線將事件傳遞到 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 且來源值為 "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}

描述
-----------
這個命令會取得 Application 事件記錄檔中來源為 Outlook 且事件識別碼為 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 月發生的所有錯誤。






請參閱




目錄