Please wrap your code with pre tags to give it a good format. For now, I have done it for you.
Coming to your query, where is the $logfile coming from? And for me, the script is executed as expected. Maybe you are noticing the progress bar flickering because the Test-Connection count is less, increase the count to see the progress bar a bit.