PowerShell: Function to Wait for Service to Be Back Online (After Server Reboots)

# Set the server name and port to test

Function waitForService{
$testSucceeded=(Test-NetConnection $server -port $port).TcpTestSucceeded

if (!($testSucceeded)){
Write-Host -NoNewline "Waiting for $server to come back online."
$timeout=300 #5 minutes
while (!($testSucceeded)) {
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
"$server is now accessible at port $port"


Leave a Reply

Your email address will not be published. Required fields are marked *