4

I need to ping things in Windows and know what time pings were received or missed; the timestamps should reflect 24-hour time (instead of an AM/PM suffix).

How can I do this in Powershell?

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
  • Possible duplicate of [Ping with timestamp](https://stackoverflow.com/questions/24906268/ping-with-timestamp) –  Jun 01 '19 at 21:04
  • 1
    PowerShell would use `Test-Connection`. From Don Jones himself. https://community.idera.com/database-tools/powershell/ask_the_experts/f/learn_powershell_from_don_jones-24/8072/test-connection-timestamp – lit Jun 01 '19 at 22:54

2 Answers2

7

You can timestamp pings wth a foreach() loop; Get-Date can take a format string:

C:\> powershell
PS C:\> ping.exe -t 4.2.2.2 | Foreach{"{0} - {1}" -f (Get-Date -f "yyyyMMdd HH:mm:ss"),$_}
20190601 14:33:03 -
20190601 14:33:03 - Pinging 4.2.2.2 with 32 bytes of data:
20190601 14:33:03 - Reply from 4.2.2.2: bytes=32 time=70ms TTL=123
20190601 14:33:04 - Reply from 4.2.2.2: bytes=32 time=71ms TTL=123
20190601 14:33:05 - Reply from 4.2.2.2: bytes=32 time=70ms TTL=123
Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
  • Nice, though note that you're using the [`ForEach-Object`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object) _cmdlet_ (via its `foreach` alias), which is distinct from the `foreach` _loop_, which cannot be used (directly) in a pipeline. Your command is short for: `ForEach-Object -Process { ... }`. – mklement0 Jun 01 '19 at 23:50
  • in linux: `PS /etc/default> ping google.com | Foreach{"{0} - {1}" -f (Get-Date -f "yyyyMMdd HH:mm:ss"),$_}` yielding: `20201029 19:26:25 - PING google.com (172.217.12.206) 56(84) bytes of data.` [...] – Yumi Koizumi Oct 29 '20 at 23:27
0

it seems that you want the display of both successful and failed pings AND that you want to handle multiple addresses. this should do the job ...[grin]

what it does ...

  • builds the list of IPs to ping
  • sets the timestamp format
    HH is for 24 hour, hh is for 12 hour, tt would be the AM or PM stuff.
  • sets the no-repsonse display line
  • sets the repeat count
    if you want this to run "forever", you can change the while to while ($True) instead of using a counter. i would just set the $RepeatCount to something like 666.
  • initialize the counter
  • define the while trigger
  • write the counter line
    if you don't want such, just remove or comment out that line.
  • loop thru the IP list with a foreach loop
  • test & store the ping result
  • generate the timestamp
  • deal with non-responding addresses & build the items to use with the -f string format operator
  • use the -f string format operator and pattern string to show the info
  • increment the counter

here's the code ...

$IP_List = @(
    '127.0.0.1'
    '10.0.0.1'
    '1.1.1.1'
    )
$TimeStampFormat = 'yyyy-MM-dd HH:mm:ss'
$NoResponse = '__No Response__'

$RepeatCount = 3

$Counter = 1
while ($Counter -le $RepeatCount)
    {
    '______ Repeat Count = {0, 3}' -f $Counter
    foreach ($IPL_Item in $IP_List)
        {
        $Result = Test-Connection -ComputerName $IPL_Item -Count 1 -ErrorAction SilentlyContinue |
            Select-Object -Property Address, ResponseTime
        $TimeStamp = [datetime]::Now.ToString($TimeStampFormat)
        if (-not $Result)
            {
            $PingData = @($TimeStamp, $env:COMPUTERNAME, $IPL_Item, $NoResponse)
            }
            else
            {
            $PingData = $($TimeStamp, $env:COMPUTERNAME, $Result.Address, $Result.ResponseTime)
            }
        '{0} - From = {1, -10} - To = {2, -16} - ResponseTime = {3, 4}' -f $PingData
        }
    $Counter++
    }

output ...

______ Repeat Count =   1
2019-06-01 21:24:50 - From = [MySysName] - To = 127.0.0.1        - ResponseTime =    0
2019-06-01 21:24:54 - From = [MySysName] - To = 10.0.0.1         - ResponseTime = __No Response__
2019-06-01 21:24:54 - From = [MySysName] - To = 1.1.1.1          - ResponseTime =   20
______ Repeat Count =   2
2019-06-01 21:24:54 - From = [MySysName] - To = 127.0.0.1        - ResponseTime =    0
2019-06-01 21:24:58 - From = [MySysName] - To = 10.0.0.1         - ResponseTime = __No Response__
2019-06-01 21:24:58 - From = [MySysName] - To = 1.1.1.1          - ResponseTime =   19
______ Repeat Count =   3
2019-06-01 21:24:58 - From = [MySysName] - To = 127.0.0.1        - ResponseTime =    0
2019-06-01 21:25:02 - From = [MySysName] - To = 10.0.0.1         - ResponseTime = __No Response__
2019-06-01 21:25:02 - From = [MySysName] - To = 1.1.1.1          - ResponseTime =   20
Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26