Thursday, April 28, 2016

This script to test your internet connection and monitor while it is down. When the connection is back up, a text notification will show up.

Scripting Language: PowerShell
This script to test your internet connection and monitor while it is down. When the connection is back up, a text notification will show up.

# This script  to test your internet connection and monitor while it is down. When the connection is back up, a text notification will show up.
# Tested on windows 7 workstation.

Try {
    $wait=5
    $site="google.com"
    While (!(Test-Connection -computer $site -count 1 -quiet)) {
            Write-Host -ForegroundColor Red -NoNewline "Connection down..."
            Start-Sleep -Seconds $wait
    }
    #Connection is good
    Write-Host -ForegroundColor Red "$(Get-Date): Connection up!" 
    $SMTPServer = "smtp.gmail.com"
    $SMTPPort = "587"
    $Username = "username@gmail.com"
    $Password = "passwors"
    $to = "to@gmail.com"
    $subject = "Email Subject"
    $body = "Insert body text here"
    $message = New-Object System.Net.Mail.MailMessage
    $message.subject = $subject
    $message.body = $body
    $message.to.add($to)
    $message.from = $username
    $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
    $smtp.EnableSSL = $true
    $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
    $smtp.send($message)
    Write-Host "Mail Sent"
    Write-Host "Successfully passed"
    exit 0
    }
Catch {
    Write-Host "Failure"
    exit 1001
      }

No comments:

Post a Comment