Powershell script to ping servers


image
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:

ServerName,IpAddress
SERVER1,10.185.1.1
SERVER2,10.185.1.2

 

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.To.Add("@")
            $msg.Subject = "Warning: ping failed on $server"
            $msg.Body = "The server $server with ip adres $ip does not reply!"
                
            $smtp.Send($msg)
            }
}
$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

9 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:

      ServerName,IpAddress
      SERVER1,10.185.1.1

    • 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:

    http://poshtips.com/2011/03/28/bgping-a-high-performance-bulk-ping-utility/

    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.

  6. ijaz says:

    #ijazahmad722@gmail.com
    #this script generate a list of ip addresses and then
    #creates a file that contain hostnames agianst that ip addresses using the ping command

    #get a list of ip addresses

    $ips = 1..254 | % {“172.17.17.$_”} > iplist.txt
    $data=Get-Content(“iplist.txt”)
    #ip to host name
    foreach ($ip in $data) {
    $ipg = ping -a -n 1 $ip | Select -Index 1
    $ipg.substring(8,$ipg.length-30) >> listofservers.txt
    }

  7. Pingback: Viktoria

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 1,211 other followers

%d bloggers like this: