Function waitForService{
$testSucceeded=(Test-NetConnection $server -port $port).TcpTestSucceeded
$null=Set-PSBreakpoint -Variable rightNow -Mode Read -Action { $global:testSucceeded = (Test-NetConnection $server -port $port).TcpTestSucceeded }

if (!($testSucceeded)){
    Write-Host -NoNewline "Waiting for $server to come back online."
    $dots=50
    $timeout=300 #5 minutes
    while (!($testSucceeded)) {
        $dots-=1;
        $timeout-=2;
        if($timeout -lt 0){"$timeout seconds have passed. Skip this waiting.";continue;}
        if ($dots -eq 0){Write-Host ".";$dots=92;}
        else {Write-Host -NoNewline "."}
        Start-Sleep -s 2
    }
    write-host "$server is now reachable via port $port" -ForegroundColor Green
}else{
    write-host "$server is reachable via port $port" -ForegroundColor Green
    }
}

waitForService