Link to home
Start Free TrialLog in
Avatar of jimmylew52
jimmylew52Flag for United States of America

asked on

Powershell script to scan log file for "ERROR" and return any results from the last 4 hours.

I have been working on a powershell script to check my log files for errors. I need to check the three log files for errors and send an email with the error that are less than 4 hours old.   This is what I have that is not working correctly:

Send-MailMessage -From 'error_log_check@mompany.com' -To 'me@companys.com' `
  -SmtpServer 'smtp.company.com' `
  -Subject 'Files Found on  server in Log Folder ' `
  -BodyAsHtml `
    -Body  @"
      The Following Files Were found On my server "`r"
     $(Get-ChildItem E:\MonitorFolder\TestBSSListener-2013-02-20.txt  | `
Select-String -Pattern error -AllMatches -SimpleMatch |
Foreach-Object {$_.Line}
out-string)
"@

The error messages are printed in the email but there is not a carriage return before the error messages and if there are multiple error messages the are strung together instead of on separate lines. I have figured out how to just report errors less than 4 hours old either. I would prefer that but can live without it.

Any help would be greatly appreciated.
Avatar of becraig
becraig
Flag of United States of America image

Here is a link dealing with the same issue:
powershellcommunity.org/Forums/tabid/54/aft/834/Default.aspx


It seems the input comes as one large string array, once you are able to strip out the messages individually you can use `r`n to enter a carriage return.
Avatar of jimmylew52

ASKER

Nothing on that site helped. The following is sort of working:

Send-MailMessage -From 'error_log_check@mompany.com' -To 'me@companys.com' `
  -SmtpServer 'smtp.company.com' `
  -Subject 'Files Found on  server in Log Folder ' `
  -BodyAsHtml `
    -Body  @"
      The Following Files Were found On my server "`r"
     $(Get-ChildItem E:\MonitorFolder\TestBSSListener-2013-02-20.txt  | `
Select-String -Pattern error -AllMatches -SimpleMatch   | ConvertTo-HTML
Foreach-Object {$_.Line}
out-string)
"@

Now I get line breaks but I get a lot of unrequested information:

Ignore Case     Line number     Pattern Context     Matches  File Name

All I want is the line and maybe the path.

Any Suggestions?
Could not get the other one to work at all. Started over and cam up with this:
$Date = (Get-Date -format yyyy-MM-dd)

$errors = Get-ChildItem E:\Monitor\Listener-$Date.txt  | `
Select-String -Pattern error -AllMatches -SimpleMatch
Foreach-Object {$_.Line}
$errors | out-file E:\Monitor\errors.txt
$linesFound = (Get-Content E:\Monitor\errors.txt)
If ($linesFound.count -gt 0)
{Send-MailMessage -From 'Log_FolderCheck_Test@companys.com' -To 'me@company.com' `
  -SmtpServer 'smtp.company.com' `
  -Subject 'Errorsfound in Log File ' `
    -Body  ((Get-Content E:\Monitor\errors.txt) | out-string)}

Gets the current date, formats it to the format we use in our log file names, checks the log file and Emails the lines of the log file with errors in them if there are any lines with errors in them..
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial