powershell ping 连续特定持续时间并保存日志
powershell ping continous for specific duration and save log
我是 powershell 的新手
在搜索测试我的互联网连接时,我发现了这个简单的代码
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"
尝试并成功,但我想将 ping 设置为特定持续时间,比如大约 30 分钟或 1 小时,所以我尝试用这个修改代码
$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"
但没有结果或输出到 txt。
我只想 ping 大约 30 分钟或 1 小时然后停止,保存结果(不仅回复而且包括 rto 和无法访问)以记录并使用任务计划安排它。任何帮助将不胜感激。
永远记住一条黄金法则:始终小心时间! (比较 times/zones、查看 past/future、时间戳等)。
同样不要使用无关的变量也是明智的。输出可以直接在脚本中重定向(参见示例)。
您的脚本 ping -t
有问题。这指定它应该查询服务器直到指定 ctrl+break。 ping
命令在 windows 上的默认行为是吐出 4 个回复。您可以使用 -n
参数更改该行为 - 循环是通过 powershell 完成的。内部 ping
循环不需要使用 -t
。
我会使用 New-TimeSpan
,如果你使用 .minutes
,这会给你带来分钟差异
编辑进一步简化脚本
现在您无需添加任何时间,只需检查当前时间是否在限制范围内。我还添加了 运行 脚本的实时时间(您可以从 ping
命令获得不同的时间戳)。
# 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)
这是缩短的日志文件:
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
我是 powershell 的新手 在搜索测试我的互联网连接时,我发现了这个简单的代码
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"
尝试并成功,但我想将 ping 设置为特定持续时间,比如大约 30 分钟或 1 小时,所以我尝试用这个修改代码
$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"
但没有结果或输出到 txt。
我只想 ping 大约 30 分钟或 1 小时然后停止,保存结果(不仅回复而且包括 rto 和无法访问)以记录并使用任务计划安排它。任何帮助将不胜感激。
永远记住一条黄金法则:始终小心时间! (比较 times/zones、查看 past/future、时间戳等)。
同样不要使用无关的变量也是明智的。输出可以直接在脚本中重定向(参见示例)。
您的脚本 ping -t
有问题。这指定它应该查询服务器直到指定 ctrl+break。 ping
命令在 windows 上的默认行为是吐出 4 个回复。您可以使用 -n
参数更改该行为 - 循环是通过 powershell 完成的。内部 ping
循环不需要使用 -t
。
我会使用 New-TimeSpan
,如果你使用 .minutes
编辑进一步简化脚本
现在您无需添加任何时间,只需检查当前时间是否在限制范围内。我还添加了 运行 脚本的实时时间(您可以从 ping
命令获得不同的时间戳)。
# 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)
这是缩短的日志文件:
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