我是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和unreachable),以便将其记录下来并与任务计划一起进行计划。任何帮助将不胜感激。
答案 0 :(得分:0)
永远记住一条黄金法则:总是小心时间! (比较时间/区域,查看过去/未来,时间戳等)。
另外,最好不要使用无关的变量。可以直接在脚本中重定向输出(请参见示例)。
您的脚本中的ping -t
有问题。这指定在指定ctrl + break之前应查询服务器。 Windows上ping
命令的默认行为是吐出4条回复。您可以使用-n
参数更改该行为-循环是通过powershell完成的。内部-t
循环无需使用ping
。
我将使用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