send only last errors on log files

Hello,

How can I modify this script for send only the last errors from a logfile :
# ChkFast Check - Version 2.0
# The script check if an error occur in the Chkfast Logs and send
# an email with the error code and the Log file in attachment.

# Chkfast variables
$LogWatchQLog = $Env:LOGschWATCHQ
$ChkFastScanLog = $Env:LOGschSCANDIR
$ChkFastServerLog = $Env:LOGschSERV
$ChkFastScriptFolder = "E:\Script\ChkFastCheck"

# Send Mail variables
$Sender = "<admin.intra>"
$Reciever1 = "<adminsupport.intra>"
$Reciever2 = "<managersupport.dba.com>"

$Subject = "Errors sending files"
$Body = get-content $ChkFastScriptFolder\Body.txt | out-string
$BodyEnd = get-content $ChkFastScriptFolder\BodyEnd.txt | out-string
$smtpServer = “smtprelay1-emea.dba.wans” 
$Encode = New-Object System.text.utf8encoding

# Check ChkFast Service
$ErrorService = ""
$ServiceList = @("schAlert", "schScandir", "schServer", "schWatchq")
$file = New-Item -type File $ChkFastScriptFolder\services.txt -force
$ServiceRapport = Get-Service -Name $ServiceList | out-file $ChkFastScriptFolder\Services.txt
Foreach ($Name in $ServiceList) {
$Service = Get-Service $Name
if ($Service.Status -eq "Stopped"){
	$ErrorService = "True"
	Start-Service $Service.Name
	}
}

# Check Log Errors and Send Mail
$BodyServices = get-content $ChkFastScriptFolder\Services.txt | out-string
$LogErrorFailed = Get-Content $LogWatchQLog | Select-String "failed" -quiet
$LogErrorBlocked = Get-Content $LogWatchQLog | Select-String "blocked" -quiet
$LogErrorEchec = Get-Content $LogWatchQLog | Select-String "Echec" -quiet
$LogErrorErreur5 = Get-Content $LogWatchQLog | Select-String "(Erreur 5)" -quiet
$LogErrorErreur6 = Get-Content $LogWatchQLog | Select-String "(Erreur 6)" -quiet
$LogErrorError = Get-Content $LogWatchQLog | Select-String "error=" -quiet
$LogErrorServerError = Get-Content $ChkFastServerLog | Select-String "error=-9002" -quiet
$LogErrorScanCant = Get-Content $ChkFastScanLog | Select-String "Can't" -quiet

if ($LogErrorFailed -eq $True -or $LogErrorBlocked -eq $True -or $LogErrorEchec -eq $True -or $LogErrorErreur5 -eq $True -or $LogErrorErreur6 -eq $True -or $LogErrorServerError -eq $True -or $LogErrorScanCant -eq $True -or $ErrorService -eq $True){ 
	$LogErrorFailed = Get-Content $LogWatchQLog | Select-String "failed" | out-string
	$LogErrorBlocked = Get-Content $LogWatchQLog | Select-String "blocked" | out-string
	$LogErrorEchec = Get-Content $LogWatchQLog | Select-String "Echec" | out-string
	$LogErrorErreur5 = Get-Content $LogWatchQLog | Select-String "(Erreur 5)" | out-string
	$LogErrorErreur6 = Get-Content $LogWatchQLog | Select-String "(Erreur 6)" | out-string
	$LogErrorError = Get-Content $LogWatchQLog | Select-String "error=" | out-string
	$LogErrorServerError = Get-Content $ChkFastServerLog | Select-String "error=-9002" | out-string
	$LogErrorScanCant = Get-Content $ChkFastScanLog | Select-String "Can't" | out-string
	
	$Body = $Body + "schwatchq.log" + $LogErrorFailed + $LogErrorBlocked + $LogErrorEchec + $LogErrorErreur5 + $LogErrorErreur6 + $LogErrorError + "schserv.log" + $LogErrorServerError + "schscandir.log" + $LogErrorScanCant + $BodyServices + $BodyEnd

	send-mailmessage -from $Sender -to $Reciever1,$Reciever2 -subject $Subject -Body $Body -smtpServer $smtpServer -Encoding $Encode
	}

Open in new window

Thanks

Best regards
bibi92Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CoralonCommented:
what are you defining as "last errors?  are you looking at particular categories, etc.?

One of the things you can do is take your variables where you did a get-content, you can do an [array]::reverse($arrayvariable) and reverse the order of the lines in the file and then read them forward, and you'll get your errors in reverse order (newest first)..

Coralon
0
bibi92Author Commented:
Hello,

I try to send an email if error occured during last hour.
Thanks
Best regards
0
CoralonCommented:
Ok.. then that's easy enough to do, based on what I'm seeing here..

Change this line:
$ServiceRapport = Get-Service -Name $ServiceList | out-file $ChkFastScriptFolder\Services.txt

Open in new window


Replace it with this:
$ServiceRapport = "[$([datetime]::Now)]  $( (Get-Service -Name $ServiceList).ToString() )" | out-file $ChkFastScriptFolder\Services.txt

Then, as you read through the file, you can add a small segment
$Now = [datetime]::Now

foreach ($Line in $LogFile)
{
     $TimeString = (select-string -pattern '\[(.*)\]' -AllMatches | foreach-object { $_.Matches }).Groups[1].value
     $Time = Get-Date $TimeString.
     $TimeSpan = new-timespan -start $now -end $time
     if ($TimeSpan.TotalMinutes -le 60) {  Send-MailMessage ... #(I'm too lazy to type this part out.. but you get it.. ) }
}

Open in new window


But, to be honest I'm not 100% sure exactly how this script is supposed to be working, but I'd think you'd want to just get the status?
(Get-Service -Name $name).Status

Coralon
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bibi92Author Commented:
thanks regards
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.