?
Solved

Powershell, summarize a txt file?

Posted on 2011-03-24
9
Medium Priority
?
672 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
SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

 
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
 
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 1000 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

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Question has a verified solution.

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

In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

601 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