PowerShell on Octopus – Verify that website is online after deploy

2015/03/25

When using Octopus deploy for your websites, you might want to verify that after deployment the website works well.
In our case, we have to restart a JBoss server (yes, we use JBoss 🙂 on a small website, the rest are under IIS).
It’s mandatory that after 5 minutes the website is back online.

The problem was that on the test environment we have an invalid HTTPS certificate (go figure !)
Solution – use:

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

Octopus PowerShell - Check if website is online

Because I’m lazy and/or I don’t have time to format the code, above is a screenshot for clarity, below is the code for whoever needs it.

# Wait 5 minutes before checking that the application is back online
Write-Host “Waiting 5 minutes for the web server to restart …”
for ($i = 0; $i -lt 10; $i++)
{
    Start-Sleep -s 30 # Sleep for 30 seconds, 10 times
    $minutes = if ($i -eq 1) { “minute” } else { “minutes” }
    Write-Host ((($i + 1) / 2).ToString() + ” $minutes passed …”)
}

$myWebsite = $OctopusParameters[‘MY_WEBSITE_URL’]
# We need the following line when accessing HTTPS site with invalid certificate !
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

$request = [System.Net.WebRequest]::Create($myWebsite)
try
{
    $response = $request.GetResponse()
    Write-Host (“The website $myWebsite is online ! Returned code: ” + $response.StatusCode)
    $response.Close()
    $response
}
catch
{
    Write-Host (“The website $myWebsite is OFFLINE ! Error: ” + $_.Exception.Message)
    throw $_.Exception
}

Note 1: Credits go to StackOverflow, IT blogs and alike, sites that helped me with the code when searching for solutions.
Note 2: Be aware that when copy-pasting, the quotes and possibly other characters get messed up by WordPress, you’ll have to replace them.


%d bloggers like this: