A script to monitor servers, Ping Test, with email notification

The Script runs against a list of Servers and notifies the user if the servers are down.

5,993 times
Add to favorites
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question

  • Script getting stuck
    2 Posts | Last post August 15, 2015
    • I ran the script with 1 in network host and 2 out of network hosts to test. But the script is getting stuck on the below code:
      # First time down create the list and send email 
                      Write-Host "Adding $_ to OutageHosts." 
                      $OutageHosts = @{$_=(get-date)} 
                      $Now = Get-date
                      $Body = "$_ has not responded for 5 pings at $Now"  
                      Send-MailMessage -Body "$body" -to $notificationto -from $notificationfrom -Subject "Host $_ is down" -SmtpServer $smtpserv
    • did you test sending the mail message from your machine?
  • SMTP settings??
    2 Posts | Last post August 15, 2015
    • I'd like to use this script on a client server to monitor the VPN status, but how are the emails supposed to be sent??  They don't have a local server to relay off of so I'll need to add an outside mail server (gmail) and user/pass in somewhere.  Where does that info need configured.  This script looks pretty sleek but you seem to be leaving out a significant chunk of info about how to actually send the emails.
    • send-mailmessage does accept a credential.
  • How would I schedule this
    2 Posts | Last post April 03, 2015
    • I added as a task but I couldn't get it to work. I have the powershell window open on one of my servers and that works. Whats the best way sql agent job run in intervals. I'm new to this Thanks 
    • I'll have to play around with the SQL agent, what version of SQL are you running?
      I normally schedule it with a standard scheduled task.
  • Linux Hosts
    2 Posts | Last post March 11, 2015
    • How could this be tweaked to test both Windows and Linux servers?
    • This just uses ICMP to make the connection with test connection. So as long as a machine will respond to ICMP this should work fine.
      You could also consider Test-WSMAN, however that uses a WSMAN query so you have to have wsman running in order to get the response from that. That is available on Linux if you install and configure OMI.
  • great...
    1 Posts | Last post October 19, 2014
  • Problem with New script, MaxOutageCount Hosts down ?
    2 Posts | Last post July 16, 2014
    • HI Ben,
      Being new to Power shell scripting,i believe second mail will trigger with all unavailable host file from $MaxOutageCount with all details, is it getting done for any one ? and any way to add CC list ? m using   
                          $ol = New-Object -comObject Outlook.Application 
                          $mail = $ol.CreateItem(0) 
                          $Mail.Subject = "More than ($MaxOutageCount Hosts down,monitoring aborted" 
                          $Mail.Body = $Body 
    • I updated the newer script to add this functionality for you:
      Now you can choose by using a switch for when you get notifications for:
      1) when a server goes down
      2) when a server comes back online
      3) when the max threshold was hit for the script to stop sending emails.
  • Email when server is back up
    2 Posts | Last post July 16, 2014
    • Hi Ben- thanks for the script- it works very nicely!
      I was wondering if you could help with the code to email when a failed server is back online? Or point me in the right direction to try and amend the script myself?
      Many thanks
    • I updated the newer script to add this functionality for you:
  • Error
    5 Posts | Last post July 19, 2012
    • I am new to powershell. I get the following error during execution of the script.Can one of you guide me
      PS D:\scripts\Alert> .\alive.ps1
      Missing expression after unary operator '-'.
      At D:\scripts\Alert\alive.ps1:56 char:20
      +                   -S <<<< ubject "Host $_ is down" -SmtpServer $smtpserver
    • Problem was with the old version of powershell. Installed powershell ver 2.0 it works fine.
      Only one error now:
      Test-Connection : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argument that is not null or empty and then try the comman
      d again.
      At C:\scripts\Alert\alive.ps1:27 char:33
        if(Test-Connection -ComputerName <<<<  $_ -Count 1 -ea silentlycontinue) 
            CategoryInfo          : InvalidData: (:) [Test-Connection], ParameterBindingValidationException
            FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand
    • One more issue in the mail I don't get the downtime.
                       $Now = Get-date 
                       $Body = "$_ has not responded for 5 pings at $Now" 
      There is some problem with the script here..
    • Hi,
      1) I had the same error, it was due to the copy and paste of the code. There is a '`' character in the line
      Send-MailMessage -Body "$body" -to $notificationto -from $notificationfrom ` 
                        -Subject "Host $_ is down" -SmtpServer $smtpserver 
      If you remove it and write the two lines as one the error is gone.
      2) Add $Now = Get-date in front of the second $Body = ... statement, the variable is assigned in the if block but was forgotten in the Else block.
      Hope this helps...
    • Hi _muris_ & bharathnazareth
      Thanks for your question and reponse. Hopefully that will help.
  • SMTP Configure
    3 Posts | Last post November 03, 2010
    • How i have to configure the smtp password and user to send mail???
      Thx a lot
    • The full help for Send-MailMessage is found by using:
      get-help Send-MailMessage -full
      The full SYNTAX from that page is:
          Send-MailMessage [-To] <string[]> [-Subject] <string> -From <string> [[-Body] <string>] 
          [[-SmtpServer] <string>] [-Attachments <string[]>] [-Bcc <string[]>] [-BodyAsHtml] [-Cc 
          <string[]>] [-Credential <PSCredential>] [-DeliveryNotificationOption {None | OnSuccess 
          | OnFailure | Delay | Never}] [-Encoding <Encoding>] [-Priority {Normal | Low | High}] [
          -UseSsl] [<CommonParameters>]
      You can see that this cmdlet uses the -credential parameter for supplying the username. If you use that parameter then it will prompt for the password, which would be a manual process for sending the mail, which is fine unless you run this using a scheduled task or by some other automation method.
      Most Enterprise Email Servers will have a Relay server setup that will accept unauthenticated relay of mail from certain host or IP Ranges. If none of these options cover your needs, you would have to use one of the many other tools that allow you to send emails and also possibly store the credentials.
    • Hi guys, 
      great script by the way for the author, my thanks for all your help. 
      Just to let you know I am using the smtpclient.send function to send the emails. here is how it looks:
       # First time down add to the list and send email
                       Write-Host "$_ Is not in the OutageHosts list, first time down"
                       $Now = get-date
                       $Body = "$_ has not responded for 5 pings at $Now" 
                       $Subject = "Host $_ is down"
                       $SMTPClient.Send($notificationfrom, $notificationto, $Subject, $Body)
                       $SMTPClient.Send($notificationfrom, $notificationto2, $Subject, $Body)
      this is the way i do it to send to 2 address because i dont know of the other way to do it, I am still learning as well. any help would be greatful 
      but thanks once again, 
      Scott the visit my site at 
  • A script to monitor servers, Ping Test, with email notification
    2 Posts | Last post August 25, 2010
    • I am new to scripting thus I will like you explian how if any way to customize this script to fit my need. I am of the opinion that is where you mention host, do I put my server name there or is it safe to say that all I need do is copy the script as is to a folder on the server to be monitored and run the scritp with task shedule?. 
      In the email to and from notification could you explain it. Can I put my exchange email address in the email to and in the email from should that be the my exchange server name?.
      Further nmore, the time specified in the scritpt fro email notification resend is it seconds or minutes?.
    • Since I designed this script to be run with a Scheduled task I made the decision not to use parameters, however I have since wanted to use this script many times by supplying parameters to monitor a chosen group of servers from the command line. Because of this I am going to updates this script to a PowerShell version 2.0 Script and include Full parameters and help.
      Now back to your question. . . . In order to provide the script with a list of server names you need to put the list of computers in the .txt file \\server\share\hosts.txt (as listed in the script).
      Since the script gets the contents of that file to use the items listed as the computers to monitor.
      You can of course simply change that path to any path that matches your needs. 
      E.g C:\Scripts\computers.txt or something similar.
      I hope that helps.
      Actually I have already updated this script so please check back in a day or so and I will upload the new version.