关于powershell的Get-WinEvent命令输出问题

cscowx 0 Reputation points
2023-01-19T01:20:05.03+00:00

powershell:Get-WinEvent -FilterHashtable @{LogName='Security';Id=5024}

输出: enter image description here

powershell:Get-WinEvent -FilterHashtable @{LogName='Security';StartTime=Get-Date;Id=4625}

输出: enter image description here

上面两张图片中,我想实际的需求如下:

  1. 如果执行powershell时有匹配的数据,我想统计所匹配数据的行数;
  2. 如果执行powershell时没有匹配的数据,会报错,我想直接返回0,不知是否可以;

请各位指导一下我,谢啦。

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,445 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,272 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Aung Zaw Min Thwin 306 Reputation points
    2023-01-19T03:28:12.5266667+00:00

    Used translate feature to understand your question. I hope the below is what you need :)

    (Get-WinEvent -FilterHashtable @{LogName='Security';StartTime=(Get-Date).Date;Id=4625} -ErrorAction SilentlyContinue | Measure-Object ).count
    
    0 comments No comments