Tuesday, January 7, 2014

PowerShell: Connectivity Monitor with Email Alert

Recently saw a post on Spiceworks that caught my eye. A poster was looking to improve a batch script by using PowerShell.  The purpose of this batch script was to test connectivity of a site based on ping.  The script worked as needed but was giving false negatives.  To resolve the poster wanted to have it test ping twice.

Below is the PowerShell script that Martin Pugh (Martin9700) provided, the thread can be viewed here: http://community.spiceworks.com/topic/427943-ping-test-script?page=1#entry-2863765

 # +-----------------------------------------------------------------------------------  
 # | File : Connectivity Monitor with Email Alert.ps1                       
 # | Version : 1.01                       
 # | Purpose : Provided CSV of IPs will test for connectivity (see comments at end)  
 # |           Ping list of IPs for connectivity  
 # |           If failed wait and repeat ping  
 # |           If failed 2nd time send alert email  
 # |        
 # | Based on: Martin Pugh (Martin9700) SpiceWorks thread  
 # |      http://community.spiceworks.com/topic/427943-ping-test-script?page=1#entry-2863455  
 # +-----------------------------------------------------------------------------------  
 # | Maintenance History                        
 # | -------------------                        
 # | Name      Date    Version C/R Description      
 # | ----------------------------------------------------------------------------------  
 # | Martin Pugh   2014-01-07   1.01     Initial scirpt build  
 # +-----------------------------------------------------------------------------------  
   
 ###SETUP START###  
      #Path to file with IP addresses  
           $Path = "[PATHTOYOURIPLISTCSVDOCUMENT]"  
      #SMTP Server to be used  
           $smtp = "{YOURSMTP]"  
      #"From" address of the email  
           $from = "ServerReports@{YOURDOMAIN].com"  
      #Enter Admin Email Address  
           $to="[YOURADMIN]@[YOURDOMAIN].com"  
 ###SETUP END###  
   
 ###PROGRAM START###  
      ForEach ($IP in (Import-CSV $Path))  
           {If (-not (Test-Connection $IP.Address -Quiet -Count 2))  
                {Start-Sleep -Seconds 2  
                     If (-not (Test-Connection $IP.Address -Quiet))  
                          {#Send Alert Email  
                               Send-MailMessage -To $to -From $from -Subject "Problem with $($IP.Location)" -Body "Problem detected with site at $($IP.Location), $($IP.Address)" -SmtpServer $smtp  
                          }  
                }  
           }  
 ###PROGRAM END###  
   
 ############################REMOVE AFTER SETUP##############################################  
 ######CSV file needs to have following format######  
 #Location,Address  
 #Sitename1,x.x.x.x  
 #Sitename2,x.x.x.x  

5 comments:

  1. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! email address

    ReplyDelete
  2. The pragmatic approach of the writer in this blog is praiseworthy.
    https://www.duocircle.com/email/outbound-smtp/

    ReplyDelete
  3. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. hotmail login account

    ReplyDelete
  4. This means that the monitors are much thinner and lighter, have an unmatched contrast ratio, colour reproduction and response time and can even be made flexible. Desktopcon's 240hz monitor

    ReplyDelete