如何使用 PowerShell 从 'Applications and Services Logs' 读取分析 Windows 事件?

How can I read analytical Windows events from 'Applications and Services Logs' using PowerShell?

我使用 Get-EventLog 来读取事件。例如,使用此 cmdlet,我可以成功读取 SystemSecurity 事件日志。我还可以通过发出以下命令列出所有可用日志:

Get-EventLog -LogName * | Select-Object -Property Log

输出:

Log
---
Application
HardwareEvents
Internet Explorer
Key Management Service
OAlerts
Parameters
Security
State
System
Windows PowerShell

但是此列表不包含您可以在 应用程序和服务日志 下找到的所有日志,例如。 g.: 我想从可以在 事件查看器:

中遍历的这条路径中读取事件
Applications and Services Logs > Microsoft > Windows > DNS-Server > Analytical

我在 Windows DNS 服务器上执行此操作,在 View 下启用 Show Analytic and Debug Logs 和还为 DNS-Server.

配置并启用了 Analytical 日志

Tl;博士:

改用Get-WinEvent并添加-Oldest参数:

Get-WinEvent -LogName Microsoft-Windows-DNSServer/Analytical -Oldest

Get-EventLog 是用于读取 Windows 事件日志的遗留 cmdlet,它不能用于读取所有可用的事件日志(在该 cmdlet 的文档中查找说明):

Get-EventLog uses a Win32 API that is deprecated. The results may not be accurate. Use the Get-WinEvent cmdlet instead.

使用 Get-WinEvent,您可以使用 Get-WinEvent -ListLog * 列出所有可用日志,或者您可以过滤所有 DNS 相关日志:

Get-WinEvent -ListLog *DNS* | Select-Object -Property LogName

输出:

LogName
-------
DNS Server
Microsoft-Windows-DNS-Client/Operational
Microsoft-Windows-DNSServer/Audit

添加 -Force 参数还可以查看 DebugAnalytical 事件:

LogName
-------
DNS Server
Microsoft-Windows-DNS-Client/Operational
Microsoft-Windows-DNSServer/Analytical
Microsoft-Windows-DNSServer/Audit

您可以通过将这些名称传递给 -LogName 参数来读取事件:

Get-WinEvent -LogName Microsoft-Windows-DNSServer/Audit

尝试读取 Microsoft-Windows-DNSServer/Analytical 日志时会出现错误:

Get-WinEvent : The Microsoft-Windows-DNSServer/Analytical event log can be read only in the forward chronological order because it is an analytical or a debug log. To see events from the Microsoft-Windows-DNSServer/Analytical event log, use the Oldest parameter in the command.

所以只需添加 -Oldest 就可以了:

Get-WinEvent -LogName Microsoft-Windows-DNSServer/Analytical -Oldest