Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following script that does a continuous ping and reports failures. It works fine, except the loop apparently "suspends" when the computer is locked. I have confirmed this by starting the script, immediately locking the PC, waiting 10 minutes, and seeing how many pings have occurred. It is nowhere near the expected number. What could be the culprit?

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("")

    if ($result.Status -eq "Success") {
        $count_dd = 0
    else {

        $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

    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."
share|improve this question
If you intend to use this as a monitoring program why don't you just schedule it and let it run in the background, you can always come back and kill it later. That way it would run in its own context all together. – Brent Pabst Jun 7 '12 at 17:02
@BrentPabst I can do that, and that is the plan, but I was curious why this script would halt execution. – tacos_tacos_tacos Jun 7 '12 at 17:41
The problem lies somewhere in the IF statement with the RawUI stuff. Without that it loops fine when locked. You could use CTRL-C to break, but then you would lose your summary stats. – Craig620 Jun 7 '12 at 18:19
Curious as to your comment - you say after 10 mins of locked console, count is nowhere near what it should be. Is it higher than a 3-4? What is the number after your test? Is it running but slowly? I wonder if the readkey is affected by the locked console. – uSlackr Jun 7 '12 at 21:59
@uSlackr Well for example if I immediately lock, and since the sleep time is 250 ms, after ten minutes I would expect 4 * 60 * 10 = at least 2400 as $total if I end script immediately. But instead I see $total is about 4 or 5, representing the time it took for me to lock and quit the program – tacos_tacos_tacos Jun 8 '12 at 0:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.