Powershell: apply multiple queries to a foreach on logs

Jherb

I need to get the total data transferred value from multiple log files. The data value is shown under the total column in the bytes row.

It is also respectively delimited with a g, m, t or k to show gb/mb/tb/kb.

           Total    Copied   Skipped  Mismatch    FAILED    Extras
Bytes :  54.414 g  54.414 g         0         0         0         0

Currently I have this script which can go through all the files and extract the value between Bytes: and the g but I need to be able to add more queries and to the `foreach file and sum them all to one consistent value.

This is what I have currently but only outputs the kb.

$pattern = "(?<=.*Bytes :.*)\w.+?(?= g.*)"
$pattern1 = "(?<=.*Bytes :.*)\w.+?(?= m.*)"
$pattern2 = "(?<=.*Bytes :.*)\w.+?(?= k.*)"

Get-ChildItem "C:\Users\logs" -Filter "BFR*" | ForEach-Object { 
    Get-Content "C:\Users\logs\*.log" | where-Object {$_ -match $pattern } | ForEach-Object {
        [double] ($matches[0])
    } | Measure-Object -Sum | Select-Object -ExpandProperty sum
} | ForEach-Object {
    Get-Content "C:\Users\logs\*.log" | where-Object {$_ -match $pattern1 } | ForEach-Object {
        [double] ($matches[0])
    } | Measure-Object -Sum | Select-Object -ExpandProperty sum
} | ForEach-Object {
Get-Content "C:\Users\logs\*.log" | where-Object {$_ -match $pattern2 } | ForEach-Object {
        [double] ($matches[0])
    } | Measure-Object -Sum | Select-Object -ExpandProperty sum
}
boxdog

Here is one possibility. (Sorry @JamesC, it's a 1-liner :-)):

Get-ChildItem .\LogFolder\*.log |
    ForEach-Object {$totalBytes = 0}{
        Get-Content $_ | Select-String -Pattern "^Bytes\s+:\s+(?<size>\d+\.\d+) (?<units>[tgmk]).*$" | 
            Foreach-Object {
                $size = $_.Matches.Groups[1].Value

                switch ($_.Matches.Groups[2].Value)
                {
                    t {$totalbytes += (1tb * $size)}
                    g {$totalbytes += (1gb * $size)}
                    m {$totalbytes += (1mb * $size)}
                    k {$totalbytes += (1kb * $size)}
                }
            }
    } {"Total Bytes: $totalBytes"}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Apply Spring JPA Specification to multiple repositories and queries

ForEach with Multiple Variables - Powershell

PowerShell Function multiple SQL queries output as CSV

Powershell foreach regarding multiple collections

MySQL Multiple UPDATE: many queries equivalent without foreach()

PowerShell multiple SQL queries, Cannot use Order by in each query

PowerShell/CLI: "Foreach" loop with multiple arrays

Is it faster to apply multiple indexes to a table one after the other or with multiple individual queries

cross apply sub queries

PowerShell: How to apply multiple filters via where-object cmdlet?

JOIN queries vs multiple queries

How to grab only the first result mysql php - multiple queries inside foreach

PowerShell Foreach Loop Not Executing after multiple successful attempts

Can you have multiple IN conditions in a PowerShell ForEach loop?

filter Foreach-Object results with multiple conditions in Powershell not working

How can I prevent from forming multiple tables in a Powershell foreach?

Foreach through XML document with Powershell returning multiple nodes

PowerShell - CSV - Multiple Headers and Values - Foreach - Use Header and Value

Multiple Logs messages in Test

Combine multiple event logs

Multiple docker container logs

Extracting sql queries from tomcat logs

Logs for SQL queries in Java using Eclipse

CloudWatch Insights queries for error logs in Lambda

How to get ' logs' of the queries in dgraph-orm

How to delete ruby logs (queries) on webpage

How can I apply multiple filters for logs in tortoise svn (e.g. revision number, filename, developer/coder)?

Multiple ElseIf queries in VBA

join multiple queries error

TOP Ranking

HotTag

Archive