Posted On April 2, 2019

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

kimconnect 2 comments
blog.KimConnect.com >> Codes >> PowerShell: Function to Wait for Service to Be Back Online (After Server Reboots)
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

2 thoughts on “PowerShell: Function to Wait for Service to Be Back Online (After Server Reboots)”

  • Hi,

    any idea how to resolve ?

    .Method invocation failed because [System.Boolean] does not contain a method named ‘Refresh’.
    At line:15 char:5
    + $testSucceeded.Refresh()
    + ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

    Tanks 🙂

    • I’ve forgotten to add a line. Retry and let me know if that works. Thanks,

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

Leave a Reply

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

Related Post

PowerShell: Get Try Catch Exception Type of a Function

# This function will return the exception type so that it could be specified on…

Microsoft SQL Cloning Database to a Different DB Name

# User defined variables $sourceSqlServer='DEV-SQL01' $sourceDatabaseName='TEST_MSCRM' $sourceSaCredential=$(get-credential) $destinationSqlServer='DEV-SQL01' $destinationDatabaseName='Test_MSCRM' $destinationSaCredential=$(get-credential) function copyDatabase{ param( $sourceSqlServer, $sourceDatabaseName,…

PowerShell: Get Spectre Meltdown Patching Versions of Hyper-V Hosts

function getHyperVHostsInForest{ function includeRSAT{ $ErrorActionPreference='stop' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #$rsatWindows7x32='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x86-RefreshPkg.msu' $rsatWindows7x64='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x64-RefreshPkg.msu' $rsatWindows81='https://download.microsoft.com/download/1/8/E/18EA4843-C596-4542-9236-DE46F780806E/Windows8.1-KB2693643-x64.msu' $rsat1709 = "https://download.microsoft.com/download/1/D/8/1D8B5022-5477-4B9A-8104-6A71FF9D98AB/WindowsTH-RSAT_WS_1709-x64.msu" $rsat1803…
Other project: DragonCoin.com