首页 > 解决方案 > PS XML 日志解析

问题描述

再会,

我从软件中得到了一个如下所示的 XML 文件:

<BRLog>
    <Log ID="id" VER="2.0">
        <Time>1609821701</Time>
        <Task>Hyper-V</Task>
        <ResultCode>03</ResultCode>
    </Log>
    <Log ID="id" VER="2.0">
        <Time>1609821802</Time>
        <Task>Files Sync</Task>
        <ResultCode>0</ResultCode>
    </Log>
    <Log ID="id" VER="2.0">
        <Time>1609822009</Time>
        <Task>RDX</Task>
        <ResultCode>23</ResultCode>
    </Log>
    <Log ID="id" VER="2.0">
        <Time>1609822701</Time>
        <Task>Hyper-V</Task>
        <ResultCode>03</ResultCode>
    </Log>
    <Log ID="id" VER="2.0">
        <Time>1609822802</Time>
        <Task>Files Sync</Task>
        <ResultCode>303</ResultCode>
    </Log>
    <Log ID="id" VER="2.0">
        <Time>1609823009</Time>
        <Task>RDX</Task>
        <ResultCode>0</ResultCode>
    </Log>
</BRLog>

我无法更改 XML 的格式。最新的元素总是添加到 XML 文件的底部。我之前不知道任务的名称。我不知道一个任务是每天运行多次、每天还是每周运行一次,因为这是在多个不同客户端和具有不同设置的环境上运行的软件的 XML 日志的输出。

我需要检查每个任务的结果代码,并报告每个任务类型的最后一个运行任务是否成功(ResultCode 0)。

我正在加载 XML 文件,然后反转整个过程以获得(现在 - 8 天)条目数:

$Logs = @()
# time settings we look now back 8 days
$now = [datetime](Get-Date)
$then = [datetime](Get-Date).AddDays(-8) 

# get file
[xml]$xml = Get-Content -Path $Path2XML
if($xml.HasChildNodes) {
    # tranform to array to use reverse function to go through xml backwards which is my forward 
    $array = $xml.BRLog.Log
    [array]::Reverse($array)
    foreach($a in $array) {
        $logtime = Convert-FromUnixDate $a.Time
        if($logTime -ge $then) {
            # Log entry is in time frame
            $a | Add-Member ScriptProperty Date {Convert-FromUnixDate $this.Time }
            $Logs += $a
            } else {
            # we now reached first entry outside of time frame, skip further selection
                break
        }
    }
}

这给了我以下输出($Logs 的内容):

Date                ID VER Time       Task       ResultCode
----                -- --- ----       ----       ----------
05.01.2021 06:03:29 id 2.0 1609823009 RDX        0
05.01.2021 06:00:02 id 2.0 1609822802 Files Sync 303
05.01.2021 05:58:21 id 2.0 1609822701 Hyper-V    03
05.01.2021 05:46:49 id 2.0 1609822009 RDX        23
05.01.2021 05:43:22 id 2.0 1609821802 Files Sync 0
05.01.2021 05:41:41 id 2.0 1609821701 Hyper-V    03

所以现在我可以看到有 3 个不同的任务:

Hyper-V - two entries, both failed - an error should be reported for this task
Files Sync - the newest one didn't work - an error should be reported for this task
RDX - the newest one worked - an info about succesful should run be reported

我现在最大的问题是当我不知道有多少任务以及它们被称为什么时,以某种方式拆分/排序任务名称,并为每个任务附加日志条目。我对对象“任务名 - 条目列表”有一个想法,但似乎无法完成。有任何想法吗?

感谢您的帮助,非常感谢!

标签: xmlpowershellparsinglogging

解决方案


Mathias R. Jessen给了我需要的提示。

group-object有按我的任务名称分组的魔力, Kevin Marquette 的这篇博文为我提供了如何正确使用它的信息。

我最终得到了以下代码,在我的if($xml.HasChildNodes)循环之后添加它。

    $Logs = $Logs | Group-Object -Property Task -AsHashTable
    foreach($key in $Logs.Keys) {
        foreach($log in $Logs[$key]) {
            if ($log.ResultCode -eq 0) {
                # 1. entry is no error, do not count
                break;
            }
            else {
                # 1. entry is error, so count
                $ErrorTexts += $log
                $ErrorCount++
                break;
            }
        }
    }

使用示例 XML 文件,我得到以下输出$ErrorTexts

Date                ID VER Time       Task       ResultCode
----                -- --- ----       ----       ----------
05.01.2021 06:00:02 id 2.0 1609822802 Files Sync 303
05.01.2021 05:58:21 id 2.0 1609822701 Hyper-V    03 

$Logs包含按任务分组的所有日志:

foreach($key in $logs.Keys) {Write-Output $Logs[$key] | format-table}

Date                ID VER Time       Task ResultCode
----                -- --- ----       ---- ----------
05.01.2021 06:03:29 id 2.0 1609823009 RDX  0
05.01.2021 05:46:49 id 2.0 1609822009 RDX  23



Date                ID VER Time       Task       ResultCode
----                -- --- ----       ----       ----------
05.01.2021 06:00:02 id 2.0 1609822802 Files Sync 303
05.01.2021 05:43:22 id 2.0 1609821802 Files Sync 0



Date                ID VER Time       Task    ResultCode
----                -- --- ----       ----    ----------
05.01.2021 05:58:21 id 2.0 1609822701 Hyper-V 03
05.01.2021 05:41:41 id 2.0 1609821701 Hyper-V 03

非常感谢,编码愉快!


推荐阅读