1
2 Comments

Checking your SSL Certificate Activation Automatically

I am currently waiting for Google to activate the SSL certificate on my custom domain name. Instead of checking every so often, I have created a small Power Shell script to do it for me every 30 minutes. If the certificate becomes valid, it opens the URL in your default browser. I am sharing the code below. You should just have to paste it in a file, and then execute the file in a Windows Power Shell interface.

# VARIABLES TO CHANGE:
# This is the number of seconds the program will wait before checking the certificate again
$intervalInSeconds = 1800
# This is the URL you want to check the certificate of.
$urlToCheck = "https://google.ca"
#################################

function Get-PublicKey {
    PARAM (
        [Uri]$Uri
    )

    if (-Not ($uri.Scheme -eq "https")) {
        Write-Error "You can only get keys for https addresses"
        return
    }

    $request = [System.Net.HttpWebRequest]::Create($uri)

    try {
        #Make the request but ignore (dispose it) the response, since we only care about the service point
        $request.GetResponse().Dispose()
        Start-Process $uri
    }
    catch [System.Net.WebException] {
        if ($_.Exception.Status -eq [System.Net.WebExceptionStatus]::TrustFailure) {
            #The ServicePoint object should now contain the Certificate for the site.
            $servicePoint = $request.ServicePoint
            # $key = $servicePoint.Certificate.GetPublicKey()
            $name = $servicePoint.Certificate.GetName()
            Write-Output $name
            throw
            #We ignore trust failures, since we only want the certificate, and the service point is still populated at this point
        }
        else {
            #Let other exceptions bubble up, or write-error the exception and return from this method
            throw
        }
    }

}

function Confirm-SslCertificate {
    param (
        [string]$URL
    )
    Write-Output "Running..."
    $doRun = 1
    while ($doRun -eq 1) {
        try {
            Get-PublicKey($URL)
            Write-Output "The certificate is correct!"
            $doRun = 0
        
        }
        catch {
            # wait 30 minutes before checking again
            $now = Get-Date -Format "HH:mm"
            $message = $now + "  Trust failure, checking again later"
            Write-Output $message
            Start-Sleep -Seconds $intervalInSeconds
        
        }
    
    }
}

Confirm-SslCertificate($urlToCheck )

Trending on Indie Hackers
How I grew a side project to 100k Unique Visitors in 7 days with 0 audience 49 comments Competing with Product Hunt: a month later 33 comments Why do you hate marketing? 28 comments My Top 20 Free Tools That I Use Everyday as an Indie Hacker 14 comments $15k revenues in <4 months as a solopreneur 14 comments Use Your Product 13 comments