Get-Event

會取得事件佇列中的事件。

語法

BySource (預設值)

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

ById

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

Description

Get-Event指令長會接收 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。

範例三:根據事件產生的時間來取得

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

指定該指令小程式接收事件的來源識別碼。 預設是事件佇列中的所有事件。 不允許使用萬用牌。

參數屬性

類型: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

此指令檔會為每個事件回傳一個 PSEventArgs 物件。 若要查看此物件的描述,請輸入 Get-Help Get-Event -Full 並查看說明主題中的備註區。

備註

Linux 或 macOS 平台沒有事件來源。

事件、事件訂閱和事件佇列只存在於當前會話中。 如果您關閉當前的會話,事件佇列將被丟棄,事件訂閱將被取消。

該 Get-Event cmdlet 回傳一個具有以下屬性的 PSEventArgs 物件(System.Management.Automation.PSEventArgs):

  • 電腦名稱。 發生事件之電腦的名稱。 這個屬性值只有在事件從遠端電腦轉發時才會被填入。

  • RunspaceId。 一個 GUID,能唯一識別事件發生的會話。 這個屬性值只有在事件從遠端電腦轉發時才會被填入。

  • 事件識別碼。 一個整數(Int32),唯一識別當前會話中的事件通知。

  • 寄件人。 產生事件的物件。 在 Action 參數的值中, $Sender 自動變數包含發送者物件。

  • SourceEventArgs。 第一個參數是從 EventArgs 衍生出來的,如果它存在的話。 例如,在一個計時器經過事件中,簽名格式為 Object sender, Timers.ElapsedEventArgs e 時, SourceEventArgs 屬性會包含 Timers.ElapsedEventArgs。 在 Action 參數的值中, $EventArgs 自動變數包含此值。

  • 資料來源:Args。 所有原始事件簽名的參數。 對於標準事件簽章,代表 $args[0] 發送端,代表 $args[1]SourceEventArgs。 在 Action 參數的值中, $args 自動變數包含此值。

  • 來源識別碼。 一個用來識別活動訂閱的字串。 在 Action 參數的值中,自動變數的 $EventSourceIdentifier 屬性包含此值。

  • TimeGenerated。 一個 DateTime 物件,代表事件產生的時間點。 在 Action 參數的值中,自動變數的 $EventTimeGenerated 屬性包含此值。

  • MessageData。 與活動訂閱相關的資料。 使用者在註冊事件時會指定這些資料。 在 Action 參數的值中,自動變數的 $EventMessageData 屬性包含此值。