Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 671
  • Last Modified:

Powershell, summarize a txt file?

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
Ben Hart
Asked:
Ben Hart
  • 6
  • 3
1 Solution
 
soostibiCommented:
Could you attach a sample file, so that I can see exactly what are the delimiters in it?
0
 
Ben HartAuthor Commented:
0
 
Ben HartAuthor Commented:
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
Ben HartAuthor Commented:
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
 
soostibiCommented:
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
 
Ben HartAuthor Commented:
Geez your good. I bow before you.
0
 
Ben HartAuthor Commented:
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
 
Ben HartAuthor Commented:
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
 
soostibiCommented:
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now