前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PS命令之中系统日志相关信息查看与管理

PS命令之中系统日志相关信息查看与管理

作者头像
全栈工程师修炼指南
发布2022-09-29 15:41:56
6030
发布2022-09-29 15:41:56
举报
文章被收录于专栏:全栈工程师修炼之路

[TOC]

系统日志查看与管理

Get-EventLog 命令 - 获取本地计算机或远程计算机上的事件日志或事件日志列表中的事件。

描述: 默认情况下Get EventLog从本地计算机获取日志,它仅适用于Windows经典事件日志,如应用程序、系统或安全性。。

Tips: 在Windows Vista和更高版本的Windows中获取使用Windows事件日志技术的日志,请使用 “get WinEvent” cmdlet。因为Get EventLog使用的Win32 API已弃用。

基础语法:

代码语言:javascript
复制
Get-EventLog [-LogName] <System.String> [[-InstanceId] <System.Int64[]>] [-After <System.DateTime>] [-AsBaseObject] [-Before <System.DateTime>] [-ComputerName <System.String[]>] [-EntryType {Error | Information | FailureAudit | SuccessAudit | Warning}] [-Index <System.Int32[]>] [-Message <System.String>] [-Newest <System.Int32>] [-Source <System.String[]>] [-UserName <System.String[]>] [<CommonParameters>]

基础示例:

代码语言:javascript
复制
# - 1.查看本机的事件日志
Get-EventLog -List
  # Max(K) Retain OverflowAction        Entries Log
  # ------ ------ --------------        ------- ---
  # 20,480      0 OverwriteAsNeeded      12,738 Application
  # 20,480      0 OverwriteAsNeeded           0 HardwareEvents
  #    512      7 OverwriteOlder              0 Internet Explorer
  # 20,480      0 OverwriteAsNeeded           0 Key Management Service
  #    128      0 OverwriteAsNeeded          64 OAlerts
  # 20,480      0 OverwriteAsNeeded      33,075 Security
  # 20,480      0 OverwriteAsNeeded      47,497 System
  # 15,360      0 OverwriteAsNeeded      10,113 Windows PowerShell

# - 2.从本地计算机上的系统事件日志获取最近的条目
Get-EventLog -LogName System -Newest 5
  # Index Time          EntryType   Source                 InstanceID Message
  #   ----- ----          ---------   ------                 ---------- -------
  #   47492 6月 21 20:53  Warning     DCOM                        10016 无法找到源“DCOM”中事件 ID“10016”的说明。

# - 3.在事件日志中查找特定数目项的所有源
$Events = Get-EventLog -LogName System -Newest 1000
$Events | Group-Object -Property Source -NoElement | Sort-Object -Property Count -Descending
  # Count Name
  # ----- ----
  #   922 Service Control Manager
  #   27 Microsoft-Windows-Kern...
  #   21 DCOM
  #   10 Microsoft-Windows-DNS-...
  #    6 Microsoft-Windows-Time...

# - 4.从特定事件日志获取错误事件(Error)、失败事件(FailureAudit)、成功事件(SuccessAudit)、Information 、Warning
Get-EventLog -LogName Security -EntryType Error

# - 5.从事件日志中获取具有InstanceId和源值的事件、或采用通配符的方式
Get-EventLog -LogName System -InstanceId 10016 -Source DCOM
  # Index Time          EntryType   Source                 InstanceID Message
  # ----- ----          ---------   ------                 ---------- -------
  # 47492 6月 21 20:53  Warning     DCOM                        10016 无法找到源“DCOM”中事件 ID“10016”的说明。  ...
  # 47
Get-EventLog -LogName System -Message *description*

# - 6.显示事件的属性值以及按属性获取事件和分组
$A = Get-EventLog -LogName System -Newest 1
$A | Select-Object -Property *
  # EventID            : 7040
  # MachineName        : WeiyiGeek
  # Data               : {}
  # Index              : 47496
  # Category           : (0)
  # CategoryNumber     : 0
  # EntryType          : Information
  # Message            : Background Intelligent Transfer Service 服务的启动类型从 自动启动 更改为 按需启动。
  # Source             : Service Control Manager
  # ReplacementStrings : {Background Intelligent Transfer Service, 自动启动, 按需启动, BITS}
  # InstanceId         : 1073748864
  # TimeGenerated      : 2021/6/21 21:23:39
  # TimeWritten        : 2021/6/21 21:23:39
  # UserName           : NT AUTHORITY\SYSTEM
  # Site               :

Get-EventLog -LogName System -UserName NT* | Group-Object -Property UserName -NoElement |
  Select-Object -Property Count, Name
  # Count  Name
  # -----  ----
  # 6031   NT AUTHORITY\SYSTEM
  #   42   NT AUTHORITY\LOCAL SERVICE
  #    4   NT AUTHORITY\NETWORK SERVICE

# - 7.获取在特定日期和时间范围内发生的事件
$Begin = Get-Date -Date '6/17/2021 08:00:00'
$End = Get-Date -Date '6/18/2021 17:00:00'
Get-EventLog -LogName System -EntryType Error -After $Begin -Before $End
  # Index Time          EntryType   Source                 InstanceID Message
  # ----- ----          ---------   ------                 ---------- -------
  # 46787 6月 18 09:21  Error       DCOM                        10001 无法找到源“DCOM”中事件 ID“10001”的说明。

# - 8.使用源和事件ID从事件日志获取事件
Get-EventLog -LogName Application -Source Outlook | Where-Object {$_.EventID -eq 63} |
 Select-Object -Property Source, EventID, InstanceId, Message
  # Source   EventID   InstanceId   Message
  # ------   -------   ----------   -------
  # Outlook       63   1073741887   The Exchange web service request succeeded.

企业实例

代码语言:javascript
复制
# (1) 系统开关机时间 (日志来源:Winlogon)
Get-EventLog -LogName System -Source * | % {if($_.EventID -eq "7001" -or $_.EventID -eq "7002"){write-host "$($_.TimeGenerated.ToString('yyyy-MM-dd HH:mm:ss')) # $($_.Message)" }}
2021-06-21 20:24:57 # 客户体验改善计划的用户登录通知
2021-06-21 14:54:53 # 客户体验改善计划的用户注销通知

(Get-WinEvent -ListLog Security).ProviderNames

您可以使用PowerShell获取与日志关联的提供程序列表。下面是一个示例,显示与安全日志关联的提供程序。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-12-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 系统日志查看与管理
    • Get-EventLog 命令 - 获取本地计算机或远程计算机上的事件日志或事件日志列表中的事件。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档