Home

Black Mountain

My Experiment on the Internet

reviewing text log entries

We use TDP for Exchange for our backup program. It's an interesting program and I will save my opinion of it for another time, but one of the things we've learned to do it to check the last 20 lines of the log file on each Exchange server daily for success/failure/issue of the backups. This process will work for checking multiple text files across several servers assuming the files are located in the same path.

I figured that the Get-Content cmdlet was the starting point I was looking for. Sure enough it got the log.

Get-Content "\\server001\c$\Program Files\Tivoli\TSM\TDPExchange\excsch.log

Big log, too much information.

I read in one of the "Hey, Scriptiing Guy! columns about filtering output using range operators like [1 .. 10] which would get the first through 10th line of text). After a review of the log files, I need the last 20 lines so [-1 .. -20] which turns out to be not the order I want. End result, start with the 20th line from the bottom and get to the last line [-20 .. -1]

(Get-Content "\\server001\c$\Program Files\Tivoli\TSM\TDPExchange\excsch.log)[-20 .. -1]

We need the information to check for success then put in a status email to the team so let's toss a first run together in what I view as the brute force approach. Just add lines for each different server..

#  Set variable for date
$d = Get-Date -UFormat "%Y%m%d"
#  Create file in current directory
New-Item -Path . -Name $d-exchlog.txt -type file

#  Add server name and line seperator in text file
"- server001 ---------------------" | Out-file .\$d-exchlog.txt -Append
" " | Out-file .\$d-exchlog.txt -Append

#  Read file with filter [-20 .. -1] <- Output last 20 lines of text
(Get-Content "\\server001\c$\Program Files\Tivoli\TSM\TDPExchange\excsch.log")[-20 .. -1] | Out-file .\$d-exchlog.txt -Append

" " | Out-file .\$d-exchlog.txt -Append
"------------------------------------" | Out-file .\$d-exchlog.txt -Append
"- server002 ---------------------" | Out-file .\$d-exchlog.txt -Append
" " | Out-file .\$d-exchlog.txt -Append
(Get-Content "\\server002\c$\Program Files\Tivoli\TSM\TDPExchange\excsch.log")[-20 .. -1] | Out-file .\$d-exchlog.txt -Append

# Launch notepad with the log file for convenience
notepad .\$d-exchlog.txt

In the end, the concept worked. Now the file path is all the same for all the servers. Also, you have to add several formatting lines each time, so let's try a more elegant solution. (Note: There is a difference between the ForEach-Object cmdlet and the foreach statement. Don't forget this like I did for a while) Due to some confusion on my part regarding the difference between the cmdlet and the foreach operator I spent some time beating my head on the desk. ForEach-Object != foreach, lesson learned. On the bright side, I learned trick to pipe the output of an array into the ForEach-Object cmdlet so all was not lost.

#        Name:  getSchedLog.ps1
#      Author:  Steven Peck
#        Date:  04/09/2008
# Description:  Get last 20 lines of exchlog.txt from exchange mailbox servers
#               Outputs to text file by date and launches file in notepad for review.
#

#  Set variable for date
$date = Get-Date -UFormat "%Y%m%d"
#  Create file in current directory
New-Item -Path . -Name $date-exchlog.txt -type file

#  Array of computers
$aryComputers = "server001","server002","server003"

$aryComputers | Foreach-Object {
  " "  | Out-file .\$date-exchlog.txt -Append
  "===================================="  | Out-file .\$date-exchlog.txt -Append
  "- $_ ---------------------"  | Out-file .\$date-exchlog.txt -Append
  " " | Out-file .\$date-exchlog.txt -Append
  (Get-Content "\\$_\c$\Program Files\Tivoli\TSM\TDPExchange\excsch.log")[-20 .. -1] | Out-file .\$date-exchlog.txt -Append
  }

# Launch notepad with the log file
notepad .\$date-exchlog.txt

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h3> <blockquote> <img>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

Sometimes I get asked, so here it is ... My Amazon.com Wish List

Thought I'd see what this Technorati stuff does.

Login with your @drupal.org user id or your OpenID to leave unmoderated comments
contents copyright Steven Peck - powered by drupal logo