关于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 for business | Windows Server | User experience | PowerShell
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

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.