Powershell script to ping servers

I created the following script to check some VM’s at home. This script pings all the servers from the CSV file. If a ping has failed, the script will send an e-mail with the servername and ip-address.

If you want to use this script, you have to change/create the following items:

  • Create a CSV file with your servers and ip-addresses
  • Enter your SMTP Server and e-mail address in the powershell script
  • Schedule the powershell script


The CSV file will contain the following information:



The Powershell script:

$servers = "D:\scripts\ps\servers.csv"
$csv = Import-CSV $servers 
$smtpServer = ""

foreach($item in $csv){
    $server = $item.ServerName
    $ip = $item.IpAddress

    $ping = new-object System.Net.NetworkInformation.Ping
    $rslt = $ping.send($ip)
        if ($rslt.status.tostring() –eq "Success") {
            write-host ping worked on $server with $ip -ForegroundColor Green}
        else {
            write-host ping failed on $server with $ip -ForegroundColor Red
            # Send E-Mail
            $msg = new-object Net.Mail.MailMessage
            $smtp = new-object Net.Mail.SmtpClient($smtpServer)
            $msg.From = "@"
            $msg.Subject = "Warning: ping failed on $server"
            $msg.Body = "The server $server with ip adres $ip does not reply!"
$ping = $null

The source of the ping script: http://theessentialexchange.com/blogs/michael/archive/2007/11/13/checking-server-availability-in-powershell.aspx

About these ads

7 Responses to Powershell script to ping servers

  1. Pingback: Episode 73 – MVP Michael Smith talks Exchange « PowerScripting Podcast

  2. Vig says:

    Doesn’t work

    Exception calling “Send” with “1″ argument(s): “Value cannot be null.
    Parameter name: hostNameOrAddress”
    At C:\srvping\servers.ps1:10 char:23
    + $rslt = $ping.send( <<<< $ip)
    You cannot call a method on a null-valued expression.
    At C:\srvping\servers.ps1:11 char:34
    + if ($rslt.status.tostring( <<<< ) -eq "Success") {

    • afokkema says:

      Do you have a CSV file like this:


    • Brian says:

      I got the same error as Vig.
      Turns out it was caused by a space after the s at the end of ServerName,IpAddress in the .csv file
      delete the space and all works well.

      Thanks for the script.

  3. Stuart says:

    Is there a way to have the output written to a .txt or .csv file? I have been able to create the file, but it is blank.

  4. Here’s an alternative that can write output to a CSV file. It can also process hundreds or even thousands of network addresses in record time and do a Name Server lookup to obtain fully qualified hostname and IpAddress:

    Check it out here:

    It does not send email, but you could easily do that using a secondary step using the code above or the Send-MailMessage cmdlet.

  5. Andrew says:

    Hi folks, does anyone know how to change the number of echo requests sent before a success or failure result is logged (and hence emailed.) Ideally I’d like it to email only if it drops 10 icmp packets in a row.

Leave a Reply


WordPress.com Logo


Twitter picture


Facebook photo


Google+ photo


Connecting to %s


Get every new post delivered to your Inbox.

Join 1,160 other followers

%d bloggers like this: