Windows Servers: Import SSL certificate on IIS and set up HTTPS binding using PowerShell

By Saad Khamis

October 30, 2021

22129 views

In this blog I will share one way to import SSL certificate on IIS and set up HTTPS binding to use the imported SSL certificate using PowerShell.

If you already imported the new SSL certificate, you can skip that step and continue with the binding step.

My blogs have relatively simple, and sometimes complex, examples and I’m hoping that you will be able to tailor them to your need or use them in your own scripts.

Prerequisites

  1. Windows IIS Server.
  2. Log on to the server with an Administrator account.

Introduction

The goal of this blog is to show one way to accomplish a task. It is not to show how to write a perfect script, the perfect solution to a challenge or the perfect process to accomplish a task.

I was asked to replace the soon to be expired SSL certificate on couple IIS servers and set up HTTPS binding to use the new SSL certificate. PowerShell scripting should make this task an easy task to accomplish.

PowerShell Cmdlets

  1. Import-Module Adds modules to the current session.
  2. WebAdministration This reference provides cmdlet descriptions and syntax for all Web Administration cmdlets. It lists the cmdlets in alphabetical order based on the verb at the beginning of the cmdlet.
  3. New-Object Creates an instance of a Microsoft .NET Framework or COM object.
  4. Get-ChildItem Gets the items and child items in one or more specified locations.
  5. Get-WebBinding Gets the bindings on an IIS site.

Manual steps to complete the task

To accomplish this task manually, the following steps need to be completed:

  1. Import PFX certificate into IIS.
  2. For each website, setup binding to use the new SSL certificate over HTTPS.

Script steps to complete the task

PowerShell script needs to do steps similar to the manual steps .

  1. Import PFX certificate into IIS. Skip this step if the new certificate is already imported into IIS.
  2. Save the certificate thumbprint if you just imported the new certificate into IIS or get the certificate thumbprint if the new certificate is already imported into IIS.
  3. For every HTTS binding, if the binding certificate expiration date is less than new certificate expiration date, bind the HTTPS to the new certificate.

Import WebAdministration Module

We need to import the WebAdministration module to interact with IIS.

Import SSL certificate on IIS & save the certificate thumbprint object

  1. Save pxf file path in a variable.
  2. Save pfx file password in a variable.
  3. Initializes a new instance of the X509Certificates class.
  4. Import certificate to the new X509Certificates class.
  5. Initializes a new instance of the X509Store class using WebHosting for StoreName and LocalMachine for StoreLocation.
  6. Open X509Store instance as ReadWrite.
  7. Add pfx certificate to the store.
  8. Close X509Store instance.
  9. Save the certificate thumbprint object, in $CertThumbprint, for a later use.

Get existing certificates in the My (Personal) and WebHosting stores

We need to get all imported/installed certificates from My (Personal) and WebHosting stores. This will be used to get more information about exiting binding.

Get the certificate thumbprint

Use Get-ChildItem to get imported certificate thumbprint from the local machine My (Personal) or WebHosting store. Save the first certificate thumbprint in the list.

Or, If you know the value of the new certificate thumbprint, search for it in all existing certificates in My (Personal) and WebHosting certificate stores on the local machine.

Bind HTTPS to use SSL certificate

  1. Get existing SSL bindings in IIS, $Bindings.
  2. Repeat the following steps for each SSL binding, $Binding .
  3. If the existing SSL binding has sites, $Binding.Sites, continue. Otherwise check the next existing SSL binding.
  4. Get the SSL binding certificate object, $BindCert.
  5. If the certificate object expiration date, $BindCert.NotAfter, is less than the new certificate expiration date, $CertThumbprint.NotAfter, continue. Otherwise, display an information message.
  6. $Binding.Sites contains either one site, string value, or multiple sites, array.
  7. If $Binding.Sites contains one site, convert the string value to an array, $Sites.
  8. For each site in $Sites, bind the site to the new SSL certificate.

Conclusion

In this blog we explored how to import SSL certificate on IIS and set up HTTPS binding to use the imported SSL certificate using PowerShell.

I hope this blog helps you work smarter, faster and more efficient. Good luck.

Did you find this blog easy to follow and helpful to you? Let me know in the comments below.

Disclaimer

Purpose of the code contained in blog is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.

Disclaimer

Purpose of the code contained in blog is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.

Comments

2 comments on “Windows Servers: Import SSL certificate on IIS and set up HTTPS binding using PowerShell

  1. Hi tried this, there are number of syntax errors, could you take a second look at this please? Specifically your value for the vairable $#91? Not valid.

Leave a Reply

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

Related Posts

List all AWS Certificate Manager certificates

By Saad Khamis 14073 views July 31, 2024

Get all AWS EBS snapshots across all profiles and regions

By Saad Khamis 18431 views June 26, 2024