Server connection test

You can use the below PowerShell script to create an email notification for server connection test. Save the below script as Test-connection.ps1 and edit the parameters. You can use any number of servers.

Test-connection.ps1

$servers = @('Server1','Server2','Server3','Server4','Server5')
$timeout = 200  #in micro-seconds

$From = "No-Reply@domain.com"
$To = @("your-email1@domain.com","your-email2@domain.com")
$SMTPServer = "smtp-server-name"

ForEach($server in $servers)
{
 if(!(Test-connection $server -count 2 -Quiet))
   {
    $Subject = "Test connection for server "+$server
    $body = $server+" is not reachable."
    Send-MailMessage -From $From -to $To -Subject $Subject -SmtpServer $SMTPServer -Body $body
    }

 elseif((Test-connection $server -count 2 | Measure-Object -Property ResponseTime -average).average -gt $timeout)
   {
    $Subject = "Test connection for server "+$server
    $body = $server+" is taking more than it's threshold limit "+$timeout+" micro-seconds."
    Send-MailMessage -From $From -to $To -Subject $Subject -SmtpServer $SMTPServer -Body $body
    }
 } 


If you want to run it using bat file then you can create a Test-connection.bat file as below. You can schedule it using Task Scheduler in Windows OS.

Test-connection.bat

Powershell.exe -file .\test-connection.ps1

Comments

Post a Comment

Popular posts from this blog

Automate Execution of Commands on Remote Linux Server/s using Linux-Automator tool

JDE Health Monitoring

Server Health Monitoring