PowerCLI

 View Only
  • 1.  Ping host and get result for X hours

    Posted Jan 30, 2018 08:48 AM

    Hello, This is relate to my previous PostPing host from ip list with powershell

    I need to use this script (if possible) to ping some hosts for X hours  for example each 5 minutes, and get result in a file,

    Result file need to have date/time and if host is Up or Down

    GC C:\Users\gemela\Desktop\Ping\hnames.txt | %{

    If (Test-Connection $_ -Quiet -Count 2){

    "$_ is UP"

    }

    Else{

    "$_ is Down"

    }

    } | Out-File C:\Users\gemela\Desktop\Ping\ping_result.txt

    Thanks



  • 2.  RE: Ping host and get result for X hours
    Best Answer

    Posted Jan 30, 2018 09:04 AM

    Try something like this

    $duration = (New-TimeSpan -Hours 1).TotalSeconds

    while($duration -gt 0){

        GC C:\Users\gemela\Desktop\Ping\hnames.txt | %{

            "$(Get-Date -Format 'yyyyMMdd HH:ss') Host: $($_)  Up: $(Test-Connection -ComputerName $_ -Quiet -Count 2)" |

            Out-File -FilePath C:\Users\gemela\Desktop\Ping\ping_result.txt -Append

        }

        sleep (5 * 60)

        $duration -= (5 * 60)

    }



  • 3.  RE: Ping host and get result for X hours

    Posted Jan 30, 2018 09:30 AM

    Many thanks again LucD,

    Script works well, I testing right now. :smileyhappy:



  • 4.  RE: Ping host and get result for X hours

    Posted Jan 30, 2018 01:44 PM

    Hi LucD, Is possible to get also on the same script the ms (millisecond), how many packets transmitted (received, loss, time?) for each line?



  • 5.  RE: Ping host and get result for X hours

    Posted Jan 30, 2018 02:28 PM

    Everything is posh-ible :smileygrin:

    Try something like this

    $duration = (New-TimeSpan -Hours 1).TotalSeconds

    $pingCount = 5

    while($duration -gt 0){

        GC C:\Users\gemela\Desktop\Ping\hnames.txt | %{

            $result = Test-Connection -ComputerName $_ -Count $pingCount -ErrorAction SilentlyContinue

            "$(Get-Date -Format 'yyyyMMdd HH:ss') Host: $($_)  " +

            "Avg Roundtrip: $([math]::Round(($result | Measure-Object -Property ResponseTime -Average | select -ExpandProperty Average),1))  " +

            "Sent: $pingCount  Received: $($result.Count) Lost: $($pingCount - $result.Count) " |

            Out-File -FilePath C:\Users\gemela\Desktop\Ping\ping_result.txt -Append

        }

        sleep (5 * 60)

        $duration -= (5 * 60)

    }



  • 6.  RE: Ping host and get result for X hours

    Posted Jan 31, 2018 12:53 PM

    Nothing impossible for you :smileyhappy:

    It works, thanks a lot LucD.