当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 获取 WinEvent (Microsoft.PowerShell.Diagnostics)

作者:精品下载站 日期:2024-12-14 02:13:29 浏览:12 分类:玩电脑

获取 WinEvent (Microsoft.PowerShell.Diagnostics)


Get-WinEvent

模块 :Microsoft.PowerShell.Diagnostics

从本地和远程计算机上的事件日志和事件跟踪日志文件中获取事件。

句法

Get-WinEvent
   [[-LogName] <String[]>]
   [-MaxEvents <Int64>]
   [-ComputerName <String>]
   [-Credential <PSCredential>]
   [-FilterXPath <String>]
   [-Force]
   [-Oldest]
   [<CommonParameters>]
Get-WinEvent
   [-ListLog] <String[]>
   [-ComputerName <String>]
   [-Credential <PSCredential>]
   [-Force]
   [<CommonParameters>]
Get-WinEvent
   [-ListProvider] <String[]>
   [-ComputerName <String>]
   [-Credential <PSCredential>]
   [<CommonParameters>]
Get-WinEvent
   [-ProviderName] <String[]>
   [-MaxEvents <Int64>]
   [-ComputerName <String>]
   [-Credential <PSCredential>]
   [-FilterXPath <String>]
   [-Force]
   [-Oldest]
   [<CommonParameters>]
Get-WinEvent
   [-Path] <String[]>
   [-MaxEvents <Int64>]
   [-Credential <PSCredential>]
   [-FilterXPath <String>]
   [-Oldest]
   [<CommonParameters>]
Get-WinEvent
   [-MaxEvents <Int64>]
   [-ComputerName <String>]
   [-Credential <PSCredential>]
   [-FilterHashtable] <Hashtable[]>
   [-Force]
   [-Oldest]
   [<CommonParameters>]
Get-WinEvent
   [-MaxEvents <Int64>]
   [-ComputerName <String>]
   [-Credential <PSCredential>]
   [-FilterXml] <XmlDocument>
   [-Oldest]
   [<CommonParameters>]

描述

此 cmdlet 仅适用于 Windows 平台。

Get-WinEvent cmdlet 从事件日志中获取事件,包括经典日志,例如系统应用程序日志。该 cmdlet 从 Windows Vista 中引入的 Windows 事件日志技术生成的事件日志以及 Windows 事件跟踪 (ETW) 生成的日志文件中的事件中获取数据。默认情况下,Get-WinEvent 按最新到最旧的顺序返回事件信息。

Get-WinEvent 列出事件日志和事件日志提供程序。要中断命令,请按 CTRL+C。您可以从选定的日志或选定的事件提供程序生成的日志中获取事件。而且,您可以在单个命令中组合来自多个源的事件。 Get-WinEvent 允许您使用 XPath 查询、结构化 XML 查询和哈希表查询来过滤事件。

如果您不是以管理员身份运行 PowerShell,您可能会看到无法检索有关日志的信息的错误消息。

示例

示例1:从本地计算机获取所有日志

此命令获取本地计算机上的所有事件日志。日志按照 Get-WinEvent 获取日志的顺序列出。首先检索经典日志,然后检索新的 Windows 事件日志。日志的 RecordCount 可能为 null,即空白或零。

Get-WinEvent -ListLog *

LogMode   MaximumSizeInBytes RecordCount LogName
-------   ------------------ ----------- -------
Circular            15532032       14500 Application
Circular             1052672         117 Azure Information Protection
Circular             1052672        3015 CxAudioSvcLog
Circular            20971520             ForwardedEvents
Circular            20971520           0 HardwareEvents

Get-WinEvent cmdlet 从计算机获取日志信息。 ListLog 参数使用星号 (*) 通配符来显示有关每个日志的信息。

示例 2:获取经典安装日志

此命令获取一个表示经典Setup日志的EventLogConfiguration对象。该对象包含有关日志的信息,例如文件大小、提供程序、文件路径以及日志是否启用。

Get-WinEvent -ListLog Setup | Format-List -Property *

FileSize                       : 69632
IsLogFull                      : False
LastAccessTime                 : 3/13/2019 09:41:46
LastWriteTime                  : 3/13/2019 09:41:46
OldestRecordNumber             : 1
RecordCount                    : 23
LogName                        : Setup
LogType                        : Operational
LogIsolation                   : Application
IsEnabled                      : True
IsClassicLog                   : False
SecurityDescriptor             : O:BAG:SYD: ...
LogFilePath                    : %SystemRoot%\System32\Winevt\Logs\Setup.evtx
MaximumSizeInBytes             : 1052672
LogMode                        : Circular
OwningProviderName             : Microsoft-Windows-Eventlog
ProviderNames                  : {Microsoft-Windows-WUSA, Microsoft-Windows-ActionQueue...
ProviderLevel                  :
ProviderKeywords               :
ProviderBufferSize             : 64
ProviderMinimumNumberOfBuffers : 0
ProviderMaximumNumberOfBuffers : 64
ProviderLatency                : 1000
ProviderControlGuid            :

Get-WinEvent cmdlet 使用ListLog 参数指定Setup 日志。该对象沿着管道发送到 Format-List cmdlet。 Format-List 使用带有星号 (*) 通配符的 Property 参数来显示每个属性。

示例3:配置经典安全日志

此命令获取一个表示经典安全日志的EventLogConfiguration对象。然后,该对象用于配置日志设置,例如最大文件大小、文件路径以及是否启用日志。

$log = Get-WinEvent -ListLog Security
$log.MaximumSizeInBytes = 1gb
try{
   $log.SaveChanges()
   Get-WinEvent -ListLog Security | Format-List -Property *
}catch [System.UnauthorizedAccessException]{
   $ErrMsg  = 'You do not have permission to configure this log!'
   $ErrMsg += ' Try running this script with administrator privileges. '
   $ErrMsg += $_.Exception.Message
   Write-Error $ErrMsg
}

FileSize                       : 69632
IsLogFull                      : False
LastAccessTime                 : 3/13/2019 09:41:46
LastWriteTime                  : 3/13/2019 09:41:46
OldestRecordNumber             : 1
RecordCount                    : 23
LogName                        : Security
LogType                        : Administrative
LogIsolation                   : Custom
IsEnabled                      : True
IsClassicLog                   : True
SecurityDescriptor             : O:BAG:SYD: ...
LogFilePath                    : %SystemRoot%\System32\Winevt\Logs\Security.evtx
MaximumSizeInBytes             : 1073741824
LogMode                        : Circular
OwningProviderName             :
ProviderNames                  : {Microsoft-Windows-WUSA, Microsoft-Windows-ActionQueue...
ProviderLevel                  :
ProviderKeywords               :
ProviderBufferSize             : 64
ProviderMinimumNumberOfBuffers : 0
ProviderMaximumNumberOfBuffers : 64
ProviderLatency                : 1000
ProviderControlGuid            :

Get-WinEvent cmdlet 使用ListLog 参数指定安全 日志。该对象被保存到变量中。对象的 MaximumSizeInBytes 属性设置为 1 GB。调用 SaveChanges 方法将更改推送到 try 块内的系统,以处理访问冲突。在 Security 日志上再次调用 Get-WinEvent cmdlet,并通过管道传输到 Format-List cmdlet 以验证 MaximumSizeInBytes

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯