• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2325
  • Last Modified:

Create powershell ping monitoring

Dear Experts,

I need help with PowerShell script to perform ping monitoring with email notification. I have list of servers stored in ServerList.txt

ServerList.txt
Server1
Server2
Server3
.
.
.

This PowerShell script will be scheduled to run every 5mins to ping list of servers stored in ServerList.txt and if any of them ping fail, email notification will be sent to Administrators.

The issue with our current ping script is that notification will send every 5mins (as the server is still down), which is very annoying.
Thus, would like to have PowerShell script with the following logic:-
1) Once server is detected ping down, only one ping down email notification to be sent.
3) Once server is up and during next schedule ping check, another ping up email notification to be sent to indicate server is back to normal.

Thanks.

Regards,
Kung Hui
0
kunghui80
Asked:
kunghui80
  • 6
  • 3
  • 2
  • +1
2 Solutions
 
Joe PriorCommented:
Hi..

You can download the required script from technet

https://gallery.technet.microsoft.com/scriptcenter/2d537e5c-b5d4-42ca-a23e-2cbce636f58d/

Thanks
Joe
0
 
kunghui80Author Commented:
Hi Joe,
I have go through the link given.  This powershell script basically monitor list of servers in single email.  

I would prefer every single host down, one email will be send just for that particular host.  If 10 servers are down, 10 email notification will be sent, and after recovery, 10 recovery email will be received to indicate server is up.

The reason is that each server ping down and every single email generated will link to our remedy system for auto ticket creation to reflect total hosts down/incident occurred.  Thanks for input.

Regards,
Kung Hui
0
 
Joe PriorCommented:
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
kunghui80Author Commented:
Hi Joe,
The link provided does not meet my requirement in question.
1) Email notification.
2) Single email alert per single host down and single recovery email when host is up.

Thanks.

Regards,
Kung Hui
0
 
becraigCommented:
Something like this should work for what you need:


gc c:\serverlist.txt | % {
$Server = $_
if (test-Connection -ComputerName $Server -Count 2 -Quiet ) {
write-Host "$Server is alive and Pinging " -ForegroundColor Green
}
else
	{
	$date = Get-Date -Format MM-dd-yyy-HH::mm
	$smtpServer = "smtp.domain.com"
    $msg = new-object Net.Mail.MailMessage
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $msg.From = "email@domain.com"
    $msg.ReplyTo = "noreply@domain.com"
    $msg.To.Add("recipient@domain.com")
    $msg.Subject = "$server Ping Monitor failure - $date "
    $msg.body = "$Server is not responding to ping."
    $smtp.Send($msg)
	}       
 
}

Open in new window

   

Replace c:\serverlist.txt  with your actual list of servers to test.
0
 
kunghui80Author Commented:
Hi Becraig,
Thanks for the feedback, by looking at content of the script, probably the following 2 scenario will surface.
1) The script will be set to run every 5mins, thus I might receive tons of alerts (Ping Down) till the server is up.
2) There isn't any recovery alert (i.e. Ping Up) sent when the host is recovered.

Thanks much!
0
 
kunghui80Author Commented:
Dear Experts,
I need some kind of logic as follow:-

The script will be scheduled to run every 5mins to detect server ping down in our network.
1) When a host is down, only one notification alert (Ping Down) per host will be sent (Not continuous flooding alerts).
2) When a host is up, only one notification alert (Ping Up) per host will be sent.

I would need one alert per host.  If hundreds host down, we will expect to receive hundreds alert too. When all are recovered, will receive hundreds of recovery email too.

Thanks all!
0
 
becraigCommented:
Ok so an easy solution would be to create a text file such and badhosts.txt  

When a server does not respond to ping then add the servername to the file.

If a server is not responding to ping  and it is present in that file then ignore.
If it is not present then add it.
If it is present and it is responding to ping then remove it.
0
 
kunghui80Author Commented:
Hi Becraig,
You've got it right, and not forgetting about email alerting portion.
Just I have no idea how to achieve that using Powershell with such logic.

Thanks!
0
 
becraigCommented:
Sorry I have had a few busy days, you want me to script in the email portion for you ?
0
 
kunghui80Author Commented:
Hi becraig, yes, would appreciate that to include email portion too :)
0
 
RobSampsonCommented:
Hi, I've come up with a modification to becraig's solution, that should satisfy your requirements, although I haven't tested it yet.

See how this goes.

Regards,

Rob.

$DownServerFile = "C:\DownServers.txt"
$FullServerFile = "C:\ServerList.txt"
# Create empty arrays for offline server lists
$PreviousDownServers = @()
$CurrentDownServers = @()
If (Test-Path -Path $DownServerFile) { $PreviousDownServers = Get-Content C:\DownServers.txt }
Get-Content -Path $FullServerFile | ForEach {
    $Server = $_
    if (test-Connection -ComputerName $Server -Count 2 -Quiet ) {
        write-Host "$Server is alive and Pinging " -ForegroundColor Green
        #Send email if this server was down previously, but is now up
        if ($PreviousDownServers -notcontains $Server) {
	        $date = Get-Date -Format MM-dd-yyy-HH::mm
	        $smtpServer = "smtp.domain.com"
            $msg = new-object Net.Mail.MailMessage
            $smtp = new-object Net.Mail.SmtpClient($smtpServer)
            $msg.From = "email@domain.com"
            $msg.ReplyTo = "noreply@domain.com"
            $msg.To.Add("recipient@domain.com")
            $msg.Subject = "$server Ping Monitor success - $date "
            $msg.body = "$Server is now responding to ping."
            $smtp.Send($msg)
        }
    }
    else
   	{
        $CurrentDownServers += $Server
        #Send email only if this is the first time the server is down
        if ($PreviousDownServers -notcontains $Server) {
	        $date = Get-Date -Format MM-dd-yyy-HH::mm
	        $smtpServer = "smtp.domain.com"
            $msg = new-object Net.Mail.MailMessage
            $smtp = new-object Net.Mail.SmtpClient($smtpServer)
            $msg.From = "email@domain.com"
            $msg.ReplyTo = "noreply@domain.com"
            $msg.To.Add("recipient@domain.com")
            $msg.Subject = "$server Ping Monitor failure - $date "
            $msg.body = "$Server is not responding to ping."
            $smtp.Send($msg)
        }
	}       
 
}
$CurrentDownServers | Out-File $DownServerFile

Open in new window

1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

  • 6
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now