取得事件佇列中的事件。

語法

Get-Event [-EventIdentifier] <int> [<CommonParameters>]

Get-Event [[-SourceIdentifier] <string>] [<CommonParameters>]

描述

Get-Event Cmdlet 會取得目前工作階段的 Windows PowerShell 事件佇列中的事件。您可以取得所有事件或使用 EventIdentifier 或 SourceIdentifier 參數指定事件。

當事件發生時會新增到事件佇列。事件佇列內包含了已註冊的事件、使用 New-Event Cmdlet 所建立的事件,以及 Windows PowerShell 結束時所引發的事件。您可以使用 Get-Event 或 Wait-Event 取得事件。

此 Cmdlet 並不會取得事件檢視器記錄檔中的事件。若要取得那些事件,請使用 Get-WinEvent 或 Get-EventLog。

參數

-EventIdentifier <int>

只取得有指定之事件識別元的事件。

必要?

true

位置?

1

預設值

接受管線輸入?

true (ByPropertyName)

接受萬用字元?

false

-SourceIdentifier <string>

只取得有指定的來源識別元的事件。預設為事件佇列中的所有事件。不允許使用萬用字元。

必要?

false

位置?

1

預設值

All events

接受管線輸入?

true (ByPropertyName)

接受萬用字元?

false

<CommonParameters>

這個 Cmdlet 支援一般參數:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。如需詳細資訊,請參閱 about_Commonparameters.

輸入和輸出

輸入型別是可經由管道輸出至 Cmdlet 的物件型別。傳回型別則是 Cmdlet 所傳回的物件型別。

輸入

您無法經由管道將輸入輸出至這個 Cmdlet。

輸出

System.Management.Automation.PSEventArgs

Get-Event 會針對每一個事件各傳回一個 PSEventArgs 物件。若要查看這個物件的描述,請輸入 "get-help get-event -full" 及查看說明主題的 Notes 區段。

附註

事件、事件訂閱和事件佇列只存在於目前的工作階段中。如果關閉目前的工作階段,則會捨棄事件佇列並取消事件訂閱。

Get-Event Cmdlet 會傳回具有下列屬性的 PSEventArgs 物件 (System.Management.Automation.PSEventArgs)。

-- ComputerName:事件發生所在的電腦名稱。只有當從遠端電腦轉送此事件時,才會填入這個屬性值。

-- RunspaceId:可唯一識別事件發生所在之工作階段的 GUID。只有當從遠端電腦轉送此事件時,才會填入這個屬性值。

-- EventIdentifier:可唯一識別目前工作階段中之事件通知的整數 (Int32)。

-- Sender:產生事件的物件。在 Action 參數的值中,$Sender 自動變數會包含 sender 物件。

-- SourceEventArgs:衍生自 EventArgs 的第一個參數 (如果存在的話)。例如,在計時器的 Elapsed 事件中 (其簽章的形式為 "Object sender, Timers.ElapsedEventArgs e"),SourceEventArgs 屬性將會包含 Timers.ElapsedEventArgs。在 Action 參數的值中,$SourceEventArgs 自動變數會包含這個值。

-- SourceArgs:原始事件簽章的所有參數。如果是標準事件簽章,$args[0] 代表 sender,$args[1] 則代表 SourceEventArgs。在 Action 參數的值中,$SourceArgs 自動變數會包含這個值。

-- SourceIdentifier:可識別事件訂閱的字串。在 Action 參數的值中,$Event 自動變數的 SourceIdentifier 屬性會包含這個值。

-- TimeGenerated:代表事件產生時間的 DateTime 物件。在 Action 參數的值中,$Event 自動變數的 TimeGenerated 屬性會包含這個值。

--MessageData:與事件訂閱有關聯的資料。當使用者註冊事件時,便會指定這個資料。在 Action 參數的值中,$Event 自動變數的 MessageData 屬性會包含這個值。

範例 1

C:\PS>get-event

描述
-----------
這個命令會取得事件佇列中的所有事件。






範例 2

C:\PS>get-event -sourceIdentifier "PowerShell.ProcessCreated"

描述
-----------
這個命令會取得 SourceIdentifier 屬性值為 "PowerShell.ProcessCreated" 的事件。






範例 3

C:\PS>$events = get-event

C:\PS> $events[0] | format-list -property *

ComputerName     :
RunspaceId       : c2153740-256d-46c0-a57c-b805917d1b7b
EventIdentifier  : 1
Sender           : System.Management.ManagementEventWatcher
SourceEventArgs  : System.Management.EventArrivedEventArgs
SourceArgs       : {System.Management.ManagementEventWatcher, System.Management.EventArrivedEventArgs}
SourceIdentifier : ProcessStarted
TimeGenerated    : 11/13/2008 12:09:32 PM
MessageData      :


C:\PS> get-event | where {$_.TimeGenerated -ge "11/13/2008 12:15:00 PM"}

ComputerName     :
RunspaceId       : c2153740-256d-46c0-a57c-b8059325d1a0
EventIdentifier  : 1
Sender           : System.Management.ManagementEventWatcher
SourceEventArgs  : System.Management.EventArrivedEventArgs
SourceArgs       : {System.Management.ManagementEventWatcher, System.Management.EventArrivedEventArgs}
SourceIdentifier : ProcessStarted
TimeGenerated    : 11/13/2008 12:15:00 PM
MessageData      :

描述
-----------
這個範例示範如何使用 SourceIdentifier 以外的屬性來取得事件。

第一個命令會取得事件佇列中的所有事件,然後將它們儲存到 $events 變數。

第二個命令會以陣列標記法來取得 $events 變數陣列中的第一個 (以零為起始的索引) 事件。第二命令會使用管線運算子 (|) 將事件傳送給 Format-List 命令,讓後者以清單方式顯示事件的所有屬性。這樣您就可以查看事件物件的屬性。

第三個命令示範如何根據事件的產生時間使用 Where-Object Cmdlet 取得事件
。






範例 4

C:\PS>get-event -eventIdentifier 2

描述
-----------
這個命令會取得事件識別碼為 2 的事件。






請參閱




目錄