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  

8 comments:

  1. 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
  2. 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
  3. Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign. hotmail entrar direto

    ReplyDelete
  4. Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for. best email marketing services

    ReplyDelete
  5. A very excellent blog post. I am thankful for your blog post. I have found a lot of approaches after visiting your post. how to send mass mail in gmail

    ReplyDelete
  6. Thisonline brand and product naming course caters to literally everyone! Small businesses or the big guns, given that they're considerate about your business and its needs, they definitely got your back! Best Email Extractor

    ReplyDelete
  7. I also wrote an article on a similar subject will find it at write what you think. Free Email Extractor Software Download

    ReplyDelete
  8. Outlook is surely an e-mail service which is made by Microsoft for personal and office use. In addition to e-mail, it comes with text messaging, calendar, contacts, etc. buy outlook accounts in bulk

    ReplyDelete