troubleshooting Question

send only last errors on log files

Avatar of bibi92
bibi92Flag for France asked on
Powershell
4 Comments1 Solution86 ViewsLast Modified:
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
	}
Thanks

Best regards
ASKER CERTIFIED SOLUTION
Coralon
Senior Citrix Engineer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros