首页 > 解决方案 > 获取 WinEvent powershell 。-包含与-匹配

问题描述

好吧,我正在处理安全事件日志,所以当我使用它-matchwhere,我等待结果,但我不明白如果我更换-match by -contains,我没有结果。

使用此代码:

$ip='52.109.12.19'
$id = 5157
Get-WinEvent  -FilterHashtable @{ LogName='security'; id=$id} | 
    Where {$_.Message -match $ip} | 
    select -Property id, message

结果是:

  Id Message                                                                                
  -- -------                                                                                
5157 La plateforme WPF (Windows Filtering Platform) a bloqué une connexion....              
5157 La plateforme WPF (Windows Filtering Platform) a bloqué une connexion....              
5157 La plateforme WPF (Windows Filtering Platform) a bloqué une connexion....              
5157 La plateforme WPF (Windows Filtering Platform) a bloqué une connexion....              
5157 La plateforme WPF (Windows Filtering Platform) a bloqué une connexion....              
5157 La plateforme WPF (Windows Filtering Platform) a bloqué une connexion....              
5157 La plateforme WPF (Windows Filtering Platform) a bloqué une connexion....              
5157 La plateforme WPF (Windows Filtering Platform) a bloqué une connexion....  

使用此代码:

$ip='52.109.12.19'
$id = 5157
Get-WinEvent  -FilterHashtable @{ LogName='security'; id=$id} | 
    Where {$_.Message -contains $ip} | 
    select -Property id, message

没有结果

我想了解为什么我没有相同的结果.....

谢谢你的帮助

标签: powershell

解决方案


Powershell-contains的工作方式类似于 JavaScript 的Array#includes.

它不会告诉您字符串是否包含某个子字符串。它只告诉您数组是否包含某个项目。

"abc" -contains "a"是。$false. "a","b","c" -contains "a"_$true

如果您想拥有子字符串级别的“包含”,请使用运算符或字符串上可用-match的 .NET方法。.Contains()

Get-WinEvent  -FilterHashtable @{ LogName='security'; id=$id} | 
    Where {$_.Message.Contains($ip)} | 
    select -Property id, message

推荐阅读