'Powershell - Set SSL Certificate on https Binding

I am trying to use PowerShell to set the SSL certificate on an IIS site for a self signed/local certificate.

I create the certificate:

$newCert = 
       New-SelfSignedCertificate 
       -DnsName www.mywebsite.ru 
       -CertStoreLocation cert:\LocalMachine\My

Then try to set the SSL bindings:

get-item 
      cert:\LocalMachine\MY\$newCert.Thumbprint | 
      new-item -path IIS:\SslBindings\0.0.0.0!443

as shown on this post: http://www.iis.net/learn/manage/powershell/powershell-snap-in-configuring-ssl-with-the-iis-powershell-snap-in

also shown here: Powershell IIS7 Snap in Assign SSL certificate to https binding

I also tried:

get-item 
      cert:\LocalMachine\MY\$newCert.Thumbprint | 
      new-item -path IIS:\SslBindings\*!443

To no avail, I'm not seeing the SSL Certificate set in the Edit Site Binding dialog.

Any thoughts?



Solution 1:[1]

I had the same error as "Chuck D" when using the answer, I found an additional step was required:

The SSL certificate needs to be in the certificate store before binding to adding to an IIS website binding. This can be done in powershell using the following command:

Import-PfxCertificate -FilePath "C:\path to certificate file\certificate.pfx" -CertStoreLocation "Cert:\LocalMachine\My"

Solution 2:[2]

The AddSslCertificate method not available everywhere. I found different solution by using Netsh.

$iisCert = Get-ChildItem -Path "Cert:\LocalMachine\MY" `
| Where-Object { $_.FriendlyName -eq "IIS Express Development Certificate" } `
| Select-Object -First 1

$applicationId = [Guid]::NewGuid().ToString("B") 

netsh http add sslcert ipport=0.0.0.0:443 certhash=$($iisCert.Thumbprint) appid=$applicationId

It is possible to set other parameters as well (e.g. certstorename=MY). More information can be found on HTTP Server API page.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Richard Squires
Solution 2 Pavel