WIndows Server IIS Web Binding Powershell

Luke Uhren 191 Reputation points
2021-05-25T15:23:53.563+00:00

I am wondering how to go about setting the web binding to a specific certificate via powershell in IIS.

Example here would be the web binding on the default web site as when I renew the cert and export it in, this will show not selected
99507-image.png

For the commands I have seen researching don't seem to work.

Example here

Import-Module Webadministration
$IPAddress = ''
$sitename = "sitename.ca"
Remove-WebBinding -Name "Default Web Site" -Protocol "https" -IPAddress $IPAddress -Port 443 -HostHeader $sitename
New-WebBinding -Name "Default Web Site" -Protocol "https" -IPAddress $IPAddress -Port 443 -HostHeader $sitename -SslFlags 1
$SSLCert = Get-ChildItem –Path "cert:\LocalMachine\My" | Where-Object {$_.subject -like 'cn=sitename.ca' -and $_.Issuer -Like "CN=R3
"}

Then the below I have seen to try and add the cert will just give the error Cannot find drive. A drive with the name 'IIS' does not exist

New-Item -Path "IIS:\SslBindings*!443!$sitename" -Value $SSLCert

What is the correct syntax to assign a SSL certificate in the edit site binding via powershell that I am missing?

Any help is greatly appreicated!

Internet Information Services
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 49,251 Reputation points
    2021-05-25T20:37:43.763+00:00

    Use the -Thumbprint argument instead of Value. Since $SSLCert is the actual cert object specify the Thumbprint property when setting it. Something like this might work but you might need to play around with it a little bit.

    New-Item -Path "..." -Thumbprint $SSLCert.Thumbprint
    
    0 comments No comments

  2. Luke Uhren 191 Reputation points
    2021-05-25T21:59:34.847+00:00

    I did try it and did not work. I have found the following and all did not work. I am on server 2012 R2 and IIS version 8.5

    New-Item -Path "IIS:\SslBindings$IPAddress!443!$sitename" -Thumbprint $thumbprint
    -error "New-Item: A parameter cannot be found that matches parameter name 'Thumbprint'"

    New-Item -Path "IIS:\SslBindings*!443!$sitename" -Value $SSLCert
    -error "New-Item: Cannot find drive. A drive with the name 'IIS' does not exist."

    netsh http add sslcert
    -error "wont allow me to use * as the ip which its set to"

    get-item cert:\LocalMachine\MY\$thumbprint | new-item $IPAddress!443
    -error "syntax is incorrect"