Avatar of bibi92
bibi92
Flag for France asked on

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
Powershell

Avatar of undefined
Last Comment
bibi92

8/22/2022 - Mon
Coralon

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
bibi92

ASKER
Hello,

I try to send an email if error occured during last hour.
Thanks
Best regards
ASKER CERTIFIED SOLUTION
Coralon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
bibi92

ASKER
thanks regards
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck