Get-Event
取得事件佇列中的事件。
語法
BySource (預設值)
Get-Event
[[-SourceIdentifier] <String>]
[<CommonParameters>]
ById
Get-Event
[-EventIdentifier] <Int32>
[<CommonParameters>]
Description
Get-Event Cmdlet 會取得目前會話之 PowerShell 事件佇列中的事件。 您可以取得所有事件,或使用 EventIdentifier 或 SourceIdentifier 參數來指定事件。
事件發生時,它會新增至事件佇列。 事件佇列包含您已註冊的事件、使用 New-Event Cmdlet 建立的事件,以及 PowerShell 結束時引發的事件。 您可以使用 Get-Event 或 Wait-Event 來取得事件。
此 Cmdlet 不會從事件查看器記錄中取得事件。 若要取得這些事件,請使用 Get-WinEvent 或 Get-EventLog。
範例
範例 1:取得所有事件
PS C:\> Get-Event
此命令會取得事件佇列中的所有事件。
範例 2:依來源標識碼取得事件
PS C:\> Get-Event -SourceIdentifier "PowerShell.ProcessCreated"
此命令會取得 SourceIdentifier 屬性值為 PowerShell.ProcessCreated 的事件。
範例 3:根據產生事件的時間取得事件
PS C:\> $Events = Get-Event
PS C:\> $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 : PS C:\> 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 變數的陣列中取得第一個 (0-index) 事件。 命令會使用管線運算符 (|) 將事件傳送至 Format-List 命令,以在清單中顯示事件的所有屬性。 這可讓您檢查事件對象的屬性。
第三個命令示範如何使用 Where-Object Cmdlet,根據產生事件的時間取得事件。
範例 4:依其標識碼取得事件
PS C:\> Get-Event -EventIdentifier 2
此命令會取得事件標識碼為 2 的事件。
參數
-EventIdentifier
指定這個 Cmdlet 取得事件的事件標識碼。
參數屬性
| 類型: | Int32 |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | 身份識別碼 |
參數集
ById
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-SourceIdentifier
指定此 Cmdlet 取得事件的來源標識碼。 默認為事件佇列中的所有事件。 不允許通配符。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
BySource
| Position: | 0 |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
CommonParameters
此 Cmdlet 支援一般參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
None
您不能將物件透過管道傳送到此 Cmdlet。
輸出
PSEventArgs
此 Cmdlet 會針對每個事件傳回 PSEventArgs 物件。 若要查看此物件的描述,請輸入 Get-Help Get-Event -Full,並參閱說明主題的 Notes 一節。
備註
事件、事件訂閱和事件佇列只存在於當前會話中。 如果您關閉當前的會話,事件佇列將被丟棄,事件訂閱將被取消。
Get-Event Cmdlet 會傳回具有下列屬性的 PSEventArgs 物件 (System.Management.Automation.PSEventArgs] 物件:
ComputerName。 發生事件之電腦的名稱。 只有當事件從遠端電腦轉送時,才會填入這個屬性值。
RunspaceId。 可唯一識別事件發生之會話的 GUID。 只有當事件從遠端電腦轉送時,才會填入這個屬性值。
EventIdentifier。 整數 (Int32), 可唯一識別目前會話中的事件通知。
寄件者。 產生事件的物件。 在 Action 參數的值中,
$Sender自動變數包含傳送者物件。SourceEventArgs。 如果事件存在,則衍生自 EventArgs 的第一個參數。 例如,在簽章窗體為 Object sender 的定時器經過事件中,Timers.ElapsedEventArgs e,SourceEventArgs 屬性會包含 Timers.ElapsedEventArgs。 在 Action 參數的值中,
$EventArgs自動變數包含此值。SourceArgs。 原始事件簽章的所有參數。 針對標準事件簽章,
$args[0]代表傳送者,$args[1]代表 SourceEventArgs。 在 Action 參數的值中,$args自動變數包含此值。SourceIdentifier。 識別事件訂閱的字串。 在 Action 參數的值中, 自動變數的
$Event屬性包含此值。TimeGenerated。 DateTime 物件,代表產生事件的時間。 在 Action 參數的值中, 自動變數的
$Event屬性包含此值。MessageData。 與事件訂閱相關聯的數據。 用戶在註冊事件時指定此數據。 在 Action 參數的值中, 自動變數的
$Event屬性包含此值。