Share via


about_Eventlogs

簡短描述

本文說明 PowerShell 如何將事件記錄至 Windows 事件記錄檔。

詳細描述

Windows PowerShell 會建立名為 “Windows PowerShell” 的 Windows 事件記錄檔,以記錄 Windows PowerShell 事件。 您可以在 事件檢視器 中檢視此記錄,或使用取得事件的 Cmdlet,例如 Get-EventLog Cmdlet。 根據預設,Windows PowerShell 引擎和提供者事件會記錄在事件記錄檔中,但您可以使用事件記錄檔喜好設定變數來自定義事件記錄檔。 例如,您可以新增 Windows PowerShell 命令的相關事件。

Windows PowerShell 事件記錄檔會記錄 Windows PowerShell 作業的詳細數據,例如啟動和停止程式引擎,以及啟動和停止 Windows PowerShell 提供者。 您也可以記錄 Windows PowerShell 命令的詳細數據。

檢視 Windows PowerShell 事件記錄檔

您可以在 事件檢視器 或使用 Get-EventLogGet-WmiObject Cmdlet 來檢視 Windows PowerShell 事件記錄檔。 若要檢視 Windows PowerShell 記錄檔的內容,請輸入:

Get-EventLog -LogName "Windows PowerShell"

若要檢查事件及其屬性,請使用 Sort-Object Cmdlet、 Group-Object Cmdlet 和包含 Format 動詞命令的 Cmdlet(Cmdlet Format )。

例如,若要檢視依事件標識碼分組的記錄檔中的事件,請輸入:

Get-EventLog "Windows PowerShell" | Format-Table -GroupBy EventID

或者,輸入:

Get-EventLog "Windows PowerShell" |
  Sort-Object EventID |
  Group-Object EventID

若要檢視所有傳統事件記錄檔,請輸入:

Get-EventLog -List

您也可以使用 Get-WmiObject Cmdlet 來使用事件相關的 Windows Management Instrumentation (WMI) 類別來檢查事件記錄檔。 例如,若要檢視事件記錄檔的所有屬性,請輸入:

Get-WmiObject Win32_NTEventlogFile |
  where LogFileName -EQ "Windows PowerShell" |
  Format-List -Property *

若要尋找 Win32 事件相關的 WMI 類別,請輸入:

Get-WmiObject -List | where Name -Like "win32*event*"

如需詳細資訊,請參閱 Get-EventLogGet-WmiObject

選取 Windows PowerShell 事件記錄檔的事件

您可以使用事件記錄檔喜好設定變數來判斷哪些事件會記錄在 Windows PowerShell 事件記錄檔中。

有六個事件記錄喜好設定變數;這三個記錄元件各有兩個變數:引擎(Windows PowerShell 程式)、提供者和命令。 LifeCycleEvent 變數會記錄一般啟動和停止事件。 健全狀況變數會記錄錯誤事件。

下表列出事件記錄檔喜好設定變數。

變數 描述
$LogEngineLifeCycleEvent 記錄 PowerShell 的啟動和停止
$LogEngineHealthEvent 記錄 PowerShell 程式錯誤
$LogProviderLifeCycleEvent 記錄 PowerShell 提供者的啟動和停止
$LogProviderHealthEvent 記錄 PowerShell 提供者錯誤
$LogCommandLifeCycleEvent 記錄命令的開始和完成
$LogCommandHealthEvent 記錄命令錯誤

(如需 Windows PowerShell 提供者的相關信息,請參閱 about_Providers

根據預設,只會啟用下列事件類型:

  • $LogEngineLifeCycleEvent
  • $LogEngineHealthEvent
  • $LogProviderLifeCycleEvent
  • $LogProviderHealthEvent

若要開啟事件類型,請將該事件類型的喜好設定變數設定為 $true。 例如,若要啟用命令生命週期事件,請輸入:

$LogCommandLifeCycleEvent

或者,輸入:

$LogCommandLifeCycleEvent = $true

若要停用事件類型,請將該事件類型的喜好設定變數設定為 $false。 例如,若要停用命令生命週期事件,請輸入:

$LogProviderLifeCycleEvent = $false

除了指出 Windows PowerShell 引擎和核心提供者已啟動的事件之外,您可以停用任何事件。 這些事件會在 Windows PowerShell 配置檔執行之前產生,以及主機程式準備好接受命令之前。

變數設定僅適用於目前的 Windows PowerShell 會話。 若要將它們套用至所有 Windows PowerShell 工作階段,請將它們新增至您的 Windows PowerShell 配置檔。

記錄模組事件

從 Windows PowerShell 3.0 開始,您可以將模組和嵌入式管理單元的 LogPipelineExecutionDetails 屬性設定$true,以記錄 Windows PowerShell 模組和函式中的 Cmdlet 和函式執行事件。 在 Windows PowerShell 2.0 中,此功能僅適用於嵌入式管理單元。

當 LogPipelineExecutionDetails 屬性值為 $true時,Windows PowerShell 會將會話中的 Cmdlet 和函式執行事件寫入 Windows PowerShell 記錄檔 事件檢視器。 此設定只有在目前的會話中才有效。

若要在模組中啟用 Cmdlet 和函式的執行事件記錄,請使用下列命令順序。

Import-Module Microsoft.PowerShell.Archive
$m = Get-Module Microsoft.PowerShell.Archive
$m.LogPipelineExecutionDetails = $true

若要在嵌入式管理單元中啟用 Cmdlet 的執行事件記錄,請使用下列命令順序。

$m = Get-PSSnapin Microsoft.PowerShell.Core
$m.LogPipelineExecutionDetails = $True

若要停用記錄,請使用相同的命令順序,將屬性值設定為 $false

您也可以使用 [開啟模組記錄 組策略] 設定來啟用和停用模組和嵌入式管理單元記錄。 原則值包含模組和嵌入式管理單元名稱的清單。 支援萬用字元。

為模組設定 [開啟模組記錄] 時,模組的LogPipelineExecutionDetails屬性值在所有$true會話中,而且無法變更。

[開啟模組記錄] 組策略設定位於下列組策略路徑中:

Computer Configuration\
  Administrative Templates\
    Windows Components\
     Windows PowerShell

User Configuration\
  Administrative Templates\
    Windows Components\
      Windows PowerShell

用戶設定原則優先於計算機設定原則,而這兩個原則都會優先於模組和嵌入式管理單元的LogPipelineExecutionDetails屬性值。

如需此組策略設定的詳細資訊,請參閱about_Group_Policy_設定

安全性和稽核

Windows PowerShell 事件記錄檔的設計目的是指出活動,並提供疑難解答的作業詳細數據。

不過,就像大部分以 Windows 為基礎的應用程式事件記錄檔一樣,Windows PowerShell 事件記錄檔並非設計為安全。 它不應該用來稽核安全性或記錄機密或專屬資訊。

事件記錄檔的設計目的是要由使用者讀取和瞭解。 用戶可以讀取和寫入記錄檔。 惡意使用者可以在本機或遠端電腦上讀取事件記錄檔、記錄 false 數據,然後防止其活動記錄。

備註

模組作者的作者可以將記錄功能新增至其模組。 如需詳細資訊,請參閱 撰寫 Windows PowerShell 模組

另請參閱