Solved

Powershell, summarize a txt file?

Posted on 2011-03-24
9
659 Views
Last Modified: 2012-06-27
Not sure of the best method to accomplish this.. but since I love Powershell I'm checking here first.

Scenario: I check the nightly backup job logs every morning, but the task of scrolling through and reading it on the server really sucks.  I can choose to export the reports as html or txt files, so I'm wondering if there's a way using PS, to pull certain information from either file type (I'm assuming txt would be easiest) and place that info into a new files or just return it to the screen.

Like for example:

Differential Backup-WedJob History for Differential Backup-Wed



Job Summary Information
Byte count          : 196,203,196,285 bytesJob rate            : 1,176.26 MB/Min (Byte count of all backup sets divided by Elapsed time for all backup sets)Files               : 23,585Directories         : 155,754Skipped files       : 0Corrupt files       : 0Files in use        : 0Original start time : Wednesday, March 16, 2011 8:00:00 PMJob started         : Wednesday, March 23, 2011 8:00:04 PMJob ended           : Wednesday, March 23, 2011 10:46:58 PMElapsed time        : 02:46:54

            Set Detail Information - Backup \\192.168.1.10\Cubes
Set type               : BackupSet status             : CompletedSet description        : Backup to disk WedResource name          : \\192.168.1.10\CubesLogon account          : System Logon AccountEncryption used        : NoneAgent used                     : YesAdvanced Open File Option used : Microsoft Volume Shadow Copy Service (VSS)


Byte count             : 1,042,936,204 bytesRate                   : 978.00 MB/MinFiles                  : 727Directories            : 25Skipped files          : 0Corrupt files          : 0Files in use           : 0Start time             : Wednesday, March 23, 2011 8:00:24 PMEnd time               : Wednesday, March 23, 2011 8:01:25 PMMedia used             : B2D000057


In this case I'm only interested in getting the following:

 Set Detail Information - Backup \\192.168.1.10\Cubes
Set status             : Completed
Resource name          : \\192.168.1.10\Cubes
Byte count             : 1,042,936,204 bytes
Rate                   : 978.00 MB/Min
Start time             : Wednesday, March 23, 2011 8:00:24 PM
End time               : Wednesday, March 23, 2011 8:01:25 PM


Is this possible?  Or is there a better method to use (i.e. vbscript or even batch)?

Thanks!
0
Comment
Question by:Ben Hart
  • 6
  • 3
9 Comments
 
LVL 13

Expert Comment

by:soostibi
Comment Utility
Could you attach a sample file, so that I can see exactly what are the delimiters in it?
0
 
LVL 14

Author Comment

by:Ben Hart
Comment Utility
0
 
LVL 14

Author Comment

by:Ben Hart
Comment Utility
Soostibi - I have to apologize.  I feel like such the idiot, for whatever reason I've never had a need to look under the reporting options for Backup Exec.  I found so many options that would satisfy what I want to do that it's seriously not funny.

You did spend time trying to help me, Admins is it ok to award him points even though he didn't specifically solve it?
0
 
LVL 14

Author Comment

by:Ben Hart
Comment Utility
Words can't express how stupid I feel right now.  I had thought that the reporting options would covere what I needed.. however after closer inspection it seems that are not able to pull information from specific instances of backup jobs.  Just the overall.  I'm not sure if that makes sense or not.. like I can generate a report based off the results, transfer rates, status, type.. any number of factors for all job histories.  But not that same info for a single job history.

So I'm back to needing to summarize or truncate the history text file.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 13

Expert Comment

by:soostibi
Comment Utility
But anyway, here is the solution, just for the sake of Power of PowerShell! :-)
Get-Content C:\Differential-Backup-Wed.txt -Encoding unicode | select-string "Set detail information" -Context 5 |
%{
    $h = @{}
    $h."Set Detail Information" = ($_.line -replace "Set Detail Information\s-","") -replace "\s{2,}"," ";
    switch -regex ($_.context.postcontext){        
        "(Set\sstatus)\W+:\s(.+?)Set" {$h.($matches[1])=$matches[2]}
        "(Resource\sname)\W+:\s(.+?)Logon" {$h.($matches[1])=$matches[2]}
        "(Byte\scount)\W+:\s(.+?)Rate" {$h.($matches[1])=$matches[2]}
        "(Rate)\W+:\s(.+?)Files" {$h.($matches[1])=$matches[2]}
        "(Start\stime)\W+:\s(.+?)End\stime" {$h.($matches[1])=$matches[2]}
        "(End\stime)\W+:\s(.+?)(Media|General)" {$h.($matches[1])=$matches[2]}
    }
    New-Object -TypeName psobject -Property $h
}

Open in new window

0
 
LVL 14

Author Comment

by:Ben Hart
Comment Utility
Geez your good. I bow before you.
0
 
LVL 14

Author Comment

by:Ben Hart
Comment Utility
Soostibi, what parts would be changed to alter the order of the results?  i.e.

resource name:
byte count:
rate:
start time:
end time:

And how hard would it be to also send the output to a new file?
0
 
LVL 14

Author Comment

by:Ben Hart
Comment Utility
I'm trying to use this script to learn from so what do the operators in that line represent?

"(Start\stime)\W+:\s(.+?)End\stime" {$h.($matches[1])=$matches[2]}

Start and End are fairly obvious, but the \W+:\s(.+?)..

0
 
LVL 13

Accepted Solution

by:
soostibi earned 250 total points
Comment Utility
Sorry, attached is the one with the right order.

The heart of the code is a switch structure. It is actually here a cycle, that takes every row from the 5 rows after the Detail Information row, and tries to find the RegEx patterns.

For example in your question:
"(Start\stime)\W+:\s(.+?)End\stime" means: find the word 'Start' plus a space '\s' and the word 'time', make this the first group within the result '(Start\stime)'. Then find non-word charaters, at least 1 time '\W+' then a colon ':' then any characters, at least 1 times and make these as the second group '(.+?)'. And accept the result only if after these there is a End-space-time text 'End\stime'.
If this structure is found, then make a property to $h as the first group found, in this case $h.("Start time"), and set for this property the value as the second group found.


Get-Content C:\ee\Differential-Backup-Wed.txt -Encoding unicode | select-string "Set detail information" -Context 5 |
%{
    $h = @{}
    $h."Set Detail Information" = ($_.line -replace "Set Detail Information\s-","") -replace "\s{2,}"," ";
    switch -regex ($_.context.postcontext){        
        "(Set\sstatus)\W+:\s(.+?)Set" {$h.($matches[1])=$matches[2]}
        "(Resource\sname)\W+:\s(.+?)Logon" {$h.($matches[1])=$matches[2]}
        "(Byte\scount)\W+:\s(.+?)Rate" {$h.($matches[1])=$matches[2]}
        "(Rate)\W+:\s(.+?)Files" {$h.($matches[1])=$matches[2]}
        "(Start\stime)\W+:\s(.+?)End\stime" {$h.($matches[1])=$matches[2]}
        "(End\stime)\W+:\s(.+?)(Media|General)" {$h.($matches[1])=$matches[2]}
    }
    New-Object -TypeName psobject -Property $h
} | Select-Object -Property "Set Detail Information", "Set status", "resource name", "byte count", "rate", "start time", "end time"

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now