How to retrieve SSL certificate of an IIS website in PowerShell

Viewed 2444

I'm trying to retrieve the certificate of a bunch of IIS websites and match the thumbprint with a certificate that I have. If the thumbprint is matched, that's perfectly fine. However, if the thumbprint is not matched then I want to add that specific certificate to that website. I know I can verify if the desired certificate exists using:

Get-ChildItem -Path Cert:\LocalMachine\My | Select-Object Thumbprint

And I can get the IIS websites and look at bindings using:

Get-ChildItem -Path IIS:Sites | Select-Object -ExpandProperty Bindings

However, I can't figure out how I can retrieve the certificate thumbprint of these websites. I have the target certificate's thumbprint stored in a variable like so:

$CertThumbprint = "###############################"

If I am going at it the wrong way, please let me know. Thank you.

1 Answers

First of all, this is not my code, found that here.
You can try this perhaps:

Import-Module WebAdministration
$siteThumbs = Get-ChildItem IIS:SSLBindings | Foreach-Object {
    [PSCustomObject]@{
        Site       = $_.Sites.Value
        Thumbprint = $_.Thumbprint
    } 
}

This should give you an array of objects with both the sites and the thumbprints for you to compare.


From your comment, I gather more than one site can share the same thumbprint and to list them separately, you could do this (untested)

Import-Module WebAdministration
$siteThumbs = Get-ChildItem IIS:SSLBindings | Foreach-Object {
    $thumb = $_.Thumbprint
    foreach ($site in $_.Sites.Value) {
        [PSCustomObject]@{
            Site       = $site
            Thumbprint = $thumb
        }
    }
}
Related