使用 Test-Connection 时如何在 Powershell 中获取 ping 时间?
How do I get ping time in Powershell when using Test-Connection?
我是 PS 脚本的新手。我的代码需要帮助。
目的:
Ping 15s内4次响应,4次响应失败
然后每隔 1 秒 ping 一次,直到得到 15 个响应,然后回到 15 秒 ping
每次ping需要用ping时间创建一个文件
这是我的代码:
$ip = "xxx.xxx.xxx.xxx"
foreach($i in 1..10){
$success = Test-Connection -Ping -IPV4 -Count 4 -TimeoutSeconds 15 -TargetName $ip -Quiet
if ($success){
Write-Host -ForegroundColor Green "Success"
}
else{
Write-Host -ForegroundColor Red "UnSuccess"
do{
Write-Host -ForegroundColor Blue "Try Again"
$success2 = Test-Connection -Ping -IPV4 -Count 15 -TimeoutSeconds 1 -TargetName $ip -Quiet
Write-Host -ForegroundColor Red "UnSuccess"
}while ($success2 -eq $false)
Write-Host -ForegroundColor Green "Try Success"
}
Write-Host -ForegroundColor Blue "Sleep 15s"
Start-Sleep -s 15
}
(Test-Connection -IPv4 '12.0.0.1').ForEach{[PSCustomObject]@{Status = $_.Status; Latency = $_.Latency}} |
Group-Object -Property Status |
ForEach-Object {
[PSCustomObject]@{
Name = $_.Name
avg = ($_.Group.Latency | Measure-Object -Average).Average
}
}
我是 PS 脚本的新手。我的代码需要帮助。 目的:
Ping 15s内4次响应,4次响应失败 然后每隔 1 秒 ping 一次,直到得到 15 个响应,然后回到 15 秒 ping
每次ping需要用ping时间创建一个文件
这是我的代码:
$ip = "xxx.xxx.xxx.xxx"
foreach($i in 1..10){
$success = Test-Connection -Ping -IPV4 -Count 4 -TimeoutSeconds 15 -TargetName $ip -Quiet
if ($success){
Write-Host -ForegroundColor Green "Success"
}
else{
Write-Host -ForegroundColor Red "UnSuccess"
do{
Write-Host -ForegroundColor Blue "Try Again"
$success2 = Test-Connection -Ping -IPV4 -Count 15 -TimeoutSeconds 1 -TargetName $ip -Quiet
Write-Host -ForegroundColor Red "UnSuccess"
}while ($success2 -eq $false)
Write-Host -ForegroundColor Green "Try Success"
}
Write-Host -ForegroundColor Blue "Sleep 15s"
Start-Sleep -s 15
}
(Test-Connection -IPv4 '12.0.0.1').ForEach{[PSCustomObject]@{Status = $_.Status; Latency = $_.Latency}} |
Group-Object -Property Status |
ForEach-Object {
[PSCustomObject]@{
Name = $_.Name
avg = ($_.Group.Latency | Measure-Object -Average).Average
}
}