Solved

Powershell, summarize a txt file?

Posted on 2011-03-24
9
661 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
ID: 35207316
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
ID: 35207369
0
 
LVL 14

Author Comment

by:Ben Hart
ID: 35207520
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
ID: 35207675
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 13

Expert Comment

by:soostibi
ID: 35207963
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
ID: 35210502
Geez your good. I bow before you.
0
 
LVL 14

Author Comment

by:Ben Hart
ID: 35210623
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
ID: 35210637
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
ID: 35211041
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

867 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

15 Experts available now in Live!

Get 1:1 Help Now