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  

3 comments:

  1. Founded in 1905, Rolex watches have been in the uk replica rolex field of innovation for more than a century. The fake cosmograph daytona watches first certified precision chronograph, the first self-winding watch and the first effective waterproof case are milestones in Rolex design. Today, the Rolex brand is immediately recognized as a symbol of global reputation, luxury and innovation. Buying a Rolex watch means you are buying a watch history. Here we have a large catalog of used Rolex special watches for sale.

    ReplyDelete
  2. The worst part of it was that the software only worked intermittently and the data was not accurate. You obviously canot confront anyone about what you have discovered if the information is not right. hotmail.com

    ReplyDelete
  3. Wow, What an Outstanding post. I found this too much informatics. It is what I was seeking for. I would like to recommend you that please keep sharing such type of info.If possible, Thanks. acesse sua conta do Hotmail entrar aqui

    ReplyDelete