使用powershell检查日志文件

使用powershell检查日志文件,powershell,Powershell,我有一个特别的任务,我不知道解决这个任务的最好办法是什么 我们的环境中有一个syslog服务器,它从服务器收集所有Windows日志 我们可以对Syslog服务器进行API调用,它返回一个带有属性“Content”(包括整个logentry)的JSON 现在我有以下情况: 我们正在VDA服务器上记录失败的登录,现在我们想计算每个在日志系统中有条目的用户在一小时内登录失败的次数 例如: 用户tes_baa有3次登录尝试失败 用户aas_eaa有2次登录尝试失败 但我的问题是,我无法轻松浏览Sysl

我有一个特别的任务,我不知道解决这个任务的最好办法是什么

我们的环境中有一个syslog服务器,它从服务器收集所有Windows日志

我们可以对Syslog服务器进行API调用,它返回一个带有属性“Content”(包括整个logentry)的JSON

现在我有以下情况: 我们正在VDA服务器上记录失败的登录,现在我们想计算每个在日志系统中有条目的用户在一小时内登录失败的次数

例如: 用户tes_baa有3次登录尝试失败 用户aas_eaa有2次登录尝试失败

但我的问题是,我无法轻松浏览Syslog服务器返回的JSON文件,因为用户名是动态的(请查看JSON文件的“Content”属性) 我的想法是创建一个正则表达式来过滤每个JSON的用户名,并将用户名添加到数组中并对数组进行计数。 为什么是正则表达式?因为每个用户名都有以下语法:“3个字符\u Name”(在sqa\u augstb下面的文件中) 或者你的想法是什么

    [20:12:38] [INF] An account failed to log on.

Subject:
    Security ID:        S-1-0-0
    Account Name:       -
    Account Domain:     -
    Logon ID:       0x0

Logon Type:         3

Account For Which Logon Failed:
    Security ID:        S-1-0-0
    Account Name:       sqa_augstb
    Account Domain:     

Failure Information:
    Failure Reason:     Unknown user name or bad password.
    Status:         0xC000006D
    Sub Status:     0xC000006A

Process Information:
    Caller Process ID:  0x0
    Caller Process Name:    -

Network Information:
    Workstation Name:   VDASW2
    Source Network Address: 10.10.2.127
    Source Port:        60973

Detailed Authentication Information:
    Logon Process:      NtLmSsp 
    Authentication Package: NTLM
    Transited Services: -
    Package Name (NTLM only):   -
    Key Length:     0

This event is generated when a logon request fails. It is generated on the computer where access was attempted.

The Subject fields indicate the account on the local system which requested the logon. This is most commonly a service such as the Server service, or a local process such as Winlogon.exe or Services.exe.

The Logon Type field indicates the kind of logon that was requested. The most common types are 2 (interactive) and 3 (network).

The Process Information fields indicate which account and process on the system requested the logon.

The Network Information fields indicate where a remote logon request originated. Workstation name is not always available and may be left blank in some cases.

The authentication information fields provide detailed information about this specific logon request.
    - Transited services indicate which intermediate services have participated in this logon request.
    - Package name indicates which sub-protocol was used among the NTLM protocols.
    - Key length indicates the length of the generated session key. This will be 0 if no session key was requested. 

在我的评论中,我对
regex
不太在行,但假设帐户名没有
或空格
\s
,您可以使用此regex:

PS /> ([regex]'\w{3}_[\w?\.?\s]+[\r\n]{1}').Matches($log)

Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 292
Length   : 17
Value    : sqa_ a..u.gu st
假设日志文件保存在变量
$log
上:

  • \w
    \w与任何单词字符匹配(相当于
    [a-zA-Z0-9\]
  • {3}
    与前一个标记精确匹配3次
  • \u
    按字面意思匹配字符
    \u
  • \w+
    在一次和无限次之间匹配任何单词字符,尽可能多地匹配,并根据需要返回(贪婪)
如果它可以有空格
\s
,则可以使用此正则表达式:

PS /> ([regex]'\w{3}_[\w?\.?\s]+[\r\n]{1}').Matches($log)

Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 292
Length   : 17
Value    : sqa_ a..u.gu st
  • 与上一个元素零或一次匹配
  • \。
    匹配文字点
  • \s
    匹配空格
  • [\w?\.?\s]+
    此组将匹配任何单词,这些单词后面可能跟一个
    \.
    \s
    在一次和无限次之间
  • [\r\n]{1}
    回车符
    新行
    精确匹配1次。我添加了这个,所以它知道在哪里停止,如果我们删除这个,它将继续匹配新的行。。。也许有更好的方法:P

必须使用
.trim()
.trimEnd()
来删除由
[\r\n]{1}

属性
内容
字符串
字符串[]
生成的尾随空格?我认为这对于任何试图帮助你的Egex noob的人来说都是很重要的,但是假设
帐户名没有
或空格,你可以使用这个
([regex]'\w{3}\uu\w+')。匹配($log)。值
和支持它可以有空格和
,你可以使用这个
([regex]'\w{3}\u124n+\r\1241]“).Matches($log).Value.trim()
$pattern=”(?m)(?:帐户名:\s+sqa_)(.$)”$日志|?{${-match$pattern}{$Matches[1]}
@SantiagoSquarzon我使用了你的正则表达式,它工作得几乎完美。请随意将其作为答案发布。多谢各位much@flaeckli在那里,我试图尽可能地解释,但再次不是一个正则表达式专家:P我修改了原始正则表达式,现在应该更好了:)