【问题标题】:Filtering/Matching eventlog records过滤/匹配事件日志记录
【发布时间】:2017-02-09 16:27:26
【问题描述】:

目标:创建一个脚本,从特定计算机获取最后一天的事件日志,然后查看消息部分以根据特定 IP 过滤数据(首先),然后仅针对以特定关键字开头的项目再次过滤该数据,然后输出到 HTML。

代码:

$fileDate = (Get-Date -Format ddMMyyyy) + ".html"
$EventGrab = Invoke-Command {
    Get-EventLog -LogName Security -After (Get-Date).AddDays(-1) -EntryType FailureAudit
} -ComputerName whatever
$EventGrab |
    Sort-Object -Property TimeWritten -Descending |
    Select-String -InputObject {$_.TimeWritten,$_.message} -Pattern "10.1.2.13" |
    ConvertTo-Html |
    Out-File C:\$fileDate

粗略的步骤:

  1. 使用日期创建文件。
  2. 使用 FailureAudit 从安全日志中获取过去 24 小时内的事件日志。
  3. 检查消息部分是否有上述模式(设备)。
  4. 转换为 HTML 并输出到包含步骤 1 中日期的文件。

问题在第 3 步和第 4 步之间;我需要弄清楚在第 3 步之后再次过滤消息,这将采用消息字段并仅在其中查找特定语法(例如 LTC),然后在找到该数据时将其输出到 HTML 文件中。

我认为它只是另一个Select-String,或者其他什么但无法弄清楚。

示例输入(@Select-String 部分):

02/08/2017 15:51:57 Network Policy Server denied access to a user.
Contact the Network Policy Server administrator for more information.
User:
Security ID:                    S-1-0-0
Account Name:                   <scrubbed>
Account Domain:                 <scrubbed>
Fully Qualified Account Name:   <scrubbed>

Client Machine:
Security ID:                    S-1-0-0
Account Name:                   -
Fully Qualified Account Name:   -
OS-Version:                     -
Called Station Identifier:      00-1B-53-41-5A-57
Calling Station Identifier:     F8-CA-B8-57-1A-9B

NAS:
NAS IPv4 Address:               10.1.2.13
NAS IPv6 Address:               -
NAS Identifier:                 -
NAS Port-Type:                  Ethernet
NAS Port:                       50324

RADIUS Client:
Client Friendly Name:           <scrubbed>
Client IP Address:              10.1.2.13

Authentication Details:
Connection Request Policy Name: <scrubbed>
Network Policy Name:            -
Authentication Provider:        Windows
Authentication Server:          <scrubbed>
Authentication Type:            PEAP
EAP Type:                       Microsoft: Secured password (EAP-MSCHAP v2)
Account Session Identifier:             -
Logging Results:                Accounting information was written to the local log file.
Reason Code:                    8
Reason:                         The specified user account does not exist.

预期输出:

以上内容已更改,因此只有帐户名称为host/LTC&lt;whatever&gt;

【问题讨论】:

    标签: powershell powershell-3.0 event-log


    【解决方案1】:

    使用Where-Object 过滤记录,使用Select-String 从消息中提取字符串:

    $EventGrab |
        Sort-Object -Property TimeWritten -Descending |
        Where-Object {
            $_.Message -like '*10.1.2.13*' -and
            $_.Message -like '*LTC*'
        } |
        Select-Object TimeWritten, @{n='Account';e={
            Select-String -InputObject $_.Message -Pattern 'Account Name:\s+(.*LTC.*)' |
                Select-Object -Expand Matches |
                ForEach-Object { $_.Groups[1].Value }
        }} |
        ConvertTo-Html |
        Out-File C:\$fileDate
    

    【讨论】:

    • 嗯。这似乎无法正常运行(没有数据输出)。我想我可能解释错了,或者我遗漏了一些东西。在来自 EventGrab 的 $_.Message 数据中,它会在该 Message 字段中返回一堆信息,我希望在大量数据中匹配 10.1.2.13 或 LTC,看起来就在上面,但我'我想知道它是否只寻找 '10.1.2.13' 和/或 'LTC' 作为字符串,而不仅仅是作为巨大消息中字符串的一部分。
    • 请编辑您的问题并提供示例输入和输出。
    • 添加了一些预期的输入/输出。
    • 由于-match 是基于正则表达式的,我会将模式更改为'10\.1\.2\.13' 或将其放入var $PatternIP = [RegEx]::Escape('10.1.2.13')。否则像 2013 年 10 月 1 日这样的日期也可以匹配。
    • @LotPings 好点。感谢您的提醒。不过,我没有转义-match 的模式,而是切换到通配符匹配。应该会提供更好的性能。
    猜你喜欢
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-27
    • 1970-01-01
    • 2011-02-09
    • 2023-02-22
    相关资源
    最近更新 更多