Send email when services are restored

Hello, I’m writing a script that will check the availability/latency for a critical web page. It can alert when slow or down, but I would also like it to send an email when services are restored but I’m not sure how…

add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

#0 = false
$hadError = 0

do
{
	try
	{
		$times = measure-command {$result = Invoke-WebRequest -Uri "www.website.com" }

		if($times.totalseconds -gt 10)
		{
			$hadError = 1 #error occured, changed to 1 or true
			
			#send email because the times are too long
			$body = "Latency is currently high, last observed time was: $($times.totalseconds) seconds"
								
			#mail on success
			$smtpServer = "1.1.1.1"
			$msg = new-object Net.Mail.MailMessage
			$smtp = new-object Net.Mail.SmtpClient($smtpServer)
			$msg.From = "Events@domain.com"
			$msg.To.Add(@("ertuu85@domain.com"))
			$msg.Subject = "High API Latency"
			$msg.Body = $body
			$smtp.Send($msg)
		}
	}
	catch
	{
                        $hadError = 1 #error occured, changed to 1 or true
			#send email because the times are too long
			$body = "Services are down`r`n`r`n Powershell Error: " + $(($error[0]).exception.message)
								
			#mail on success
			$smtpServer = "1.1.1.1"
			$msg = new-object Net.Mail.MailMessage
			$smtp = new-object Net.Mail.SmtpClient($smtpServer)
			$msg.From = "Events@domain.com"
			$msg.To.Add(@("ertuu85@domain.com"))
			$msg.Subject = "Down"
			$msg.Body = $body
			$smtp.Send($msg)
	}
	
	#somehow here it would be like
	if($haderror -eq 0)
	{
		#send email, all issues resolved
	}
	
	start-sleep 60
}until(1 -eq 2)

I would assume, in this case, $hadError would have to equal 1 or whatever if an error triggers but i’m not sure…because if an error doesnt trigger i dont want an email sending out every loop saying ‘all issues resolved’ or something.

Any help would be greatly appreciated

You probably don’t want it just firing off emails saying “all’s well!” all the time. So what your’e going to have to do is set a flag - either a variable, or more ideally a file on disk - to indicate that a problem occurred. Then, when the script checks the status, if the flag exists and the status is okay, it clears the flag and sends an email.

Thanks for the reply, how would I put a flag in this so it would NOT ping each time? It’s easy enough to make $haderror = 1 on error, but when it’s fixed it will just trigger each time, not sure how to proceed.

Thanks again for the help

When you fix it, you need to set $haderror back to $false or 0 or whatever.

E.g., “if there is no error, and $haderror -eq 1, then set $haderror = 0 and send an all-clear email”