powershell ping continous for specific duration and save log

谁都会走 提交于 2021-01-29 08:09:06

问题


I am new to powershell when searching for test my internet connection i found this simple code

    ping google.com -t | Select-String "Reply" | foreach $_ { $a = Get-Date$a.ToString() + " " + $_ } | Out-File  "C:\users\"your account"\Documents\pingLog$((get-date).tostring("HHmmss")).txt"

try it and success but i want to set ping for specific duration say about 30 minutes or 1 hour so i try modified the code with this

     $stopTime = (Get-Date).AddMinutes(30)
     $results = do {
     $now = Get-Date
     ping google.com -t | Select-String "Reply" | foreach $_ { $a = Get-Date
     $a.ToString() + " " + $_ } 
     } 
     until ($now -ge $stopTime)

     $results | Out-File  "C:\users\"your account"\Documents\pingLog$((get-date).tostring("HHmmss")).txt"

but no result or output to txt.

i just want to ping for about 30 minutes or 1 hours and stop, save the result(not only reply but include rto and unreachable) to log and schedule it with task schedule. Any help would be greatly appreciated.


回答1:


Remember always a golden rule: Be always careful with time! (comparing times/zones, looking into past/future, timestamps etc.).

Also it is wise not to use extraneous variables. The output can be redirected directly in the script (see the example).

Your script has an issue with the ping -t. That specifies that it should query the server till ctrl+break is specified. The default behaviour of ping command on windows is to spit out 4 replies. You can change that beaviour with -n parameter - the looping is done via powershell. There is no need to use -t for the internal ping loop.

I would use New-TimeSpan which gives you minutes difference if you use .minutes

Edit further simplification of the script

Now you don't need to add any time just check if current time is within the limit. I have added also the real time of running the script (you can get different time stamp from ping command).

 # 1 minute
 $limit_in_minutes = 1
 # path to log
 $log_file = '<path_to_log>\time.log'
 # clear the log file before running again
 fc > $log_file

 $start_timer = Get-Date
 Do {
    $current_time = Get-Date
    ping google.com -n 1 | Select-String "Reply" | foreach $_ { $a = Get-Date; $a.ToString() + " " + $_ } | Out-File -Append -Encoding UTF8 -FilePath  $log_file
    # normally the output of New-TimeSpan is a String but you need an Integer to be able to compare it
    $running_minutes = [int]((New-TimeSpan –Start $start_timer –End $current_time).minutes) 
    $running_seconds = [int]((New-TimeSpan –Start $start_timer –End $current_time).seconds) 
    Write-Output  "Running for: $($running_minutes)m:$($running_seconds)s" | Out-File -Append -Encoding UTF8 -FilePath  $log_file
 } Until ($running_minutes -ge $limit_in_minutes)

Here is shortened log file:

   11.09.2018 16:10:35 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
    Running for: 0m:0s
    11.09.2018 16:10:35 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
    Running for: 0m:0s
    11.09.2018 16:10:35 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
    Running for: 0m:0s
    11.09.2018 16:10:35 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
    Running for: 0m:0s
    11.09.2018 16:10:36 Reply from 172.217.168.78: bytes=32 time=47ms TTL=57
    Running for: 0m:0s
    11.09.2018 16:10:36 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
    Running for: 0m:1s
    11.09.2018 16:10:36 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
    Running for: 0m:1s
    11.09.2018 16:10:36 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
    Running for: 0m:1s
    11.09.2018 16:10:36 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
    Running for: 0m:1s

...

Running for: 0m:58s
11.09.2018 16:11:33 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
Running for: 0m:58s
11.09.2018 16:11:34 Reply from 172.217.168.78: bytes=32 time=47ms TTL=57
Running for: 0m:58s
11.09.2018 16:11:34 Reply from 172.217.168.78: bytes=32 time=47ms TTL=57
Running for: 0m:59s
11.09.2018 16:11:34 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
Running for: 0m:59s
11.09.2018 16:11:34 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
Running for: 0m:59s
11.09.2018 16:11:34 Reply from 172.217.168.78: bytes=32 time=47ms TTL=57
Running for: 0m:59s
11.09.2018 16:11:35 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
Running for: 0m:59s
11.09.2018 16:11:35 Reply from 172.217.168.78: bytes=32 time=47ms TTL=57
Running for: 1m:0s


来源:https://stackoverflow.com/questions/52273215/powershell-ping-continous-for-specific-duration-and-save-log

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!