我有以下脚本,它可以连续执行 ping 并报告失败。它工作正常,只是当计算机锁定时循环似乎会“暂停”。我已通过启动脚本、立即锁定 PC、等待 10 分钟并查看已发生多少次 ping 来确认这一点。它远没有达到预期的数量。罪魁祸首可能是什么?
Write-Host "Entering monitoring loop..." -Background DarkRed
$ping = new-object System.Net.NetworkInformation.Ping
$count_up = 0
$count_dn = 0
$count_dd = 0
while ($true) {
$result = $ping.send("10.1.1.1")
if ($result.Status -eq "Success") {
$count_up++
$count_dd = 0
}
else {
$count_dn++
$count_dd++
$this_date = Get-Date
Write-Host "VPN ping failed at time " $this_date -Background Magenta
if ($count_dd -gt 3) {
Write-Host "***VPN is Down***" `a
send_mail_notification("VPN is Down", "")
}
}
if ($Host.UI.RawUI.KeyAvailable -and ("q" -eq $Host.UI.RawUI.ReadKey("IncludeKeyUp,NoEcho").Character)) {
Write-Host "Exiting monitoring loop..." -Background DarkRed
break;
}
Start-Sleep -m 250
}
$total = $count_up + $count_dn
$uptime = 100 * $count_up / $total
Write-Host $count_up " out of " $total " pings for a " $uptime "% uptime."