jimmylew52
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.c om' -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\TestBSSLi stener-201 3-02-20.tx t | `
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.
Send-MailMessage -From 'error_log_check@mompany.c
-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\TestBSSLi
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.
ASKER
Nothing on that site helped. The following is sort of working:
Send-MailMessage -From 'error_log_check@mompany.c om' -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\TestBSSLi stener-201 3-02-20.tx t | `
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?
Send-MailMessage -From 'error_log_check@mompany.c
-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\TestBSSLi
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?
ASKER
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@comp anys.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..
$Date = (Get-Date -format yyyy-MM-dd)
$errors = Get-ChildItem E:\Monitor\Listener-$Date.
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@comp
-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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
powershellcommunity.org/Fo
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.