none
Powershell script to ping a list of IP address and email name if down.

    Question

  • I am trying to ping a list of IP Addresses and if no response email the list of non-pinging IP addresses and the name of that device. I want to import a CSV or Excel File that has the IP address in the first column and the name of the device is the coresponding second column... (A1 IP Address, B1 Name). This is what I have:

    $missing = gc "C:\<list>.csv" |?{ (gwmi Win32_PingStatus -Filter "Address='$_'").StatusCode -ne 0 }
    If ($missing -ne $null)
    {
    $body = "No Ping response was recieved from $missing . Please check device(s) for problems"
    Send-MailMessage -SmtpServer "SMTP SERVER NAME" -From "FROM ADDRESS" -To "TO ADDRESS" -Subject "TITLE!" -Body $body
    }

    I feel lost but I am new to Powershell... Thank you in advance

    Sunday, August 26, 2012 3:09 AM

Answers

  • It's not something you see very often, but it works well, and avoids having to create and explicitly accumulate the objects into an array, arraylist, or collection. The script block will output them to the pipeline, and you can just pick them up at the end.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Sunday, August 26, 2012 12:34 PM
  • If you use a script block, you don't need that intermediate array at all:

    &{
    $ipcsv = Import-Csv 'ipCSV.csv'
    foreach ($ip in $ipcsv) {
        $ok = Test-Connection $ip.ipaddress -Count 1 -Quiet 
        if (-not($ok)) {
            $hostn = [System.Net.Dns]::GetHostEntry($ip.ipaddress).HostName
            New-Object -TypeName PSObject -Property @{'Host'=$hostn;'IP'=$ip.ipaddress}
            }
        }
    } | Export-CSV -Path 'failed.csv' -notype


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Sunday, August 26, 2012 12:10 PM

All replies

  • I suggest you visit our script repository. 

    http://gallery.technet.microsoft.com/scriptcenter

    and the Script Center home page:

    http://technet.microsoft.com/en-us/scriptcenter/bb410849.aspx


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Sunday, August 26, 2012 3:32 AM
  • This may work:

    $failed = @()
    $ipcsv = Import-Csv 'ipCSV.csv'
    foreach ($ip in $ipcsv) {
        $ok = Test-Connection $ip.ipaddress -Count 1 -Quiet 
        if (-not($ok)) {
            $hostn = [System.Net.Dns]::GetHostEntry($ip.ipaddress).HostName
            $failed += New-Object -TypeName PSObject -Property @{'Host'=$hostn;'IP'=$ip.ipaddress}
            }
        }
    $failed | Export-CSV -Path 'failed.csv' -notype
    

    This assumes you have a proper csv file, with a column named "ipaddress".

    Grant Ward, a.k.a. Bigteddy



    • Edited by Bigteddy Sunday, August 26, 2012 11:52 AM Code fix
    Sunday, August 26, 2012 9:41 AM
  • Test-Connection does nt generate an exception when -quiet is used.  It is designed to return a boolean so it can be used like this:

    Import-Csv 'ipCSV.csv' |
        %{
          $prop=@{
                  Host=$_.IpAddress
                  Status='UnKnown'
                  HostName=[System.Net.Dns]::GetHostEntry($ip.ipaddress).HostName
                 }
          if($result=Test-Connection $i_.ipaddress -Count 1 -Quiet){
               $prop.Status='Online'
          }
          New-Object PSOblect -Property $prop
        }
    	

    This is a common design pattern   that is used for this kind of module.


    ¯\_(ツ)_/¯

    Sunday, August 26, 2012 11:41 AM
  • OK, thanks for pointing that out.  I started my testing without the Quiet, and just added that at the end.  Afterwards I realised my mistake, but sort of forgot about it.

    Grant Ward, a.k.a. Bigteddy

    Sunday, August 26, 2012 11:49 AM
  • If you use a script block, you don't need that intermediate array at all:

    &{
    $ipcsv = Import-Csv 'ipCSV.csv'
    foreach ($ip in $ipcsv) {
        $ok = Test-Connection $ip.ipaddress -Count 1 -Quiet 
        if (-not($ok)) {
            $hostn = [System.Net.Dns]::GetHostEntry($ip.ipaddress).HostName
            New-Object -TypeName PSObject -Property @{'Host'=$hostn;'IP'=$ip.ipaddress}
            }
        }
    } | Export-CSV -Path 'failed.csv' -notype


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Sunday, August 26, 2012 12:10 PM
  • That's interesting.  Are you enclosing the whole script in &{ }, and then piping the script results to csv?  I've never seen something like that before.

    Grant Ward, a.k.a. Bigteddy

    Sunday, August 26, 2012 12:27 PM
  • It's not something you see very often, but it works well, and avoids having to create and explicitly accumulate the objects into an array, arraylist, or collection. The script block will output them to the pipeline, and you can just pick them up at the end.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Sunday, August 26, 2012 12:34 PM
  • I know that this is an older post, but I am getting an error when it is reading in the file. Here is my modified script.

    &{
    $ipcsv = Import-Csv 'C:\Users\Administrator\ipCSV.csv' -header("IP")
    foreach ($ip in $ipcsv) {
        $ok = Test-Connection $ip.ipaddress -Count 1 -Quiet
        if (-not(I$ok)) {
            $hostn = [System.Net.Dns]::GetHostEntry($ip.ipaddress).HostName
            New-Object -TypeName PSObject -Property @{'Host'=$hostn;'IP'=$ip.ipaddress}
            }
        }
    ## Export the list of bad IP addresses
        } | Export-CSV -Path 'failed.csv' -notype

    The error I get is  Test-Connection : Cannot validate argument on parapeter 'ComputerName".  The argument is null or empty.  Supply an argument that is not null or empty and then try the command again. 

    This leads me to believe that it is not reading in the file correctly.  I have a plain csv with just ipv4 addresses.

    Help is much appreciated!


    Tony DiSalvo

    Friday, February 15, 2013 3:08 PM