How to extract information from a text file?
I have a series of Log files (text format) from various servers with columns of information. I have two PowerShell scripts (below) which seek and calculate details from certain columns and outputs a text file.
Script #1 (Combined RTP Packet(s) lost)
This script will look at the "Details" column in the log file and calculate all the " XX RTP Packet(s) have been lost" (e.g. 57 RTP Packet(s) have been lost)
The script can be executed in any directory or folder that contains (*.log) files and will give you a total amount of RTP Packet (s) lost.
$sum = 0
foreach ($i in dir -filter *.log -Rec)
{
$sum += (gc $i.fullname | select -Skip 5 | ConvertFrom-Csv -Delimiter "`t" | ? {$_.Details - match "^(\d+)"} |% {$matches[1]} | Measure-Object -Sum).Sum
}
$sum | Tee-Object -FilePath .\CombinedResults.txt
[Console]::Write("Press any key to continue . . . ")
[Console]::ReadKey()
Script #2 (Total RTP Packet(s) lost by Entity)
This script will look at the "Details" column in the log file and calculate all the " XX RTP Packet(s) have been lost" (eg. 57 RTP Packet(s) have been lost) it than will show you the total packets lost per Entity (camera) the entity name is found in the "Entity" Column.
The script can be executed in any directory or folder that contains (*.log) files and will give you a total amount of RTP Packet(s) lost per entity.
$out=foreach ($i in dir -filter *.log -Rec)
{
$cameras = gc $i.fullname | select -Skip 5 | ConvertFrom-Csv -Delimiter "`t" | group "Entity "
$cameras | select Name, @{n="Total";e={ ($_.group | ? {$_.Details -match "^(\d+)"} |% {$matches [1]} | Measure-Object -Sum).Sum}} | ? {$_.Total -gt 0}
}
$out | Tee-Object -FIlePath .\ByEntityResults.txt
[Console]::Write("Press any key to continue . . . ")
[Console]::ReadKey()
Here is what I'm trying to achieve:
These log files come from various servers, and I want results to be filtered by server. In the log files on the top left corner it states "Computer Name:"
For both scripts I would like the results to be filtered by Computer Name.
End Result Example:
Script # 1
Computer Name: T3-Archiver22
Total RTP Packet(s) lost = 43243
Computer Name: T3-Archiv开发者_Go百科er24
Total RTP Packet(s) lost = 8837
Script # 2
Computer Name: T3-Archiver22
Entity Name: Total RTP Packet(s) lost
(7233)C2-GF-127 - 10.20.13.69(P) 54
.
.
.
Computer Name: T3-Archiver24
Entity Name: Total RTP Packet(s) lost
(6547)U2 Z-C14 27
.
.
.
Instead of accumulating a single value, such as a $sum, you can accumulate sums associated with a server name using hashtables (associative arrays, dictionaries), then write out the collected data from the table to a file.
There's a few examples of hashtable usage in this link:
Does powershell have associative arrays?
精彩评论