title: Assign IIS SSL Certificate to Binding with Host Header using PowerShell
body: <p>I'm trying to assign a certificate to a HTTPS binding.
  Unfortunately, I get this error from PowerShell:</p>
  
  <pre><code>new-item : Cannot create a file when that file already exists
  At line:3 char:56
  +         get-item -Path "cert:\localmachine\my\$cert" | new-item -path IIS:\SslBi ...
  +                                                        ~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [New-Item], Win32Exception
      + FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.NewItemCommand
  </code></pre>
  
  <p>My PowerShell which I execute is:</p>
  
  <pre><code> New-WebBinding -name $Name -Protocol https -HostHeader "$Name.domain.com" -Port 443 -SslFlags 1
   $cert = Get-ChildItem -Path Cert:\LocalMachine\My | where-Object {$_.subject -like "*cloud.domain.com*"} | Select-Object -ExpandProperty Thumbprint
   get-item -Path "cert:\localmachine\my\$cert" | new-item -path IIS:\SslBindings\0.0.0.0!443!$Name.domain.com
  </code></pre>
  
  <p>It seems to be able to find the certificate, but is not able to assign it to the created binding.
  The binding gets created with the right IP/Port/HostHeader, SNI is checked, but SSL Certificate is "Not selected"</p>
  
  <p>It all works fine from IIS Manager</p>
  
  <p>I have tried various instructions from SO and other sites, e.g.:<br>
  <a href="http://technet.microsoft.com/en-us/magazine/jj871065.aspx" rel="noreferrer">http://technet.microsoft.com/en-us/magazine/jj871065.aspx</a><br>
  <a href="https://stackoverflow.com/questions/2628970/powershell-iis7-snap-in-assign-ssl-certificate-to-https-binding">Powershell IIS7 Snap in Assign SSL certificate to https binding</a><br>
  <a href="https://stackoverflow.com/questions/19120339/powershell-add-ssl-binding-using-shared-certificate">Powershell - Add SSL binding using shared certificate</a></p>
  
  <p>Also, I have tried with </p>
  
  <pre><code>IIS:\SslBindings\0.0.0.0!443!$Name.domain.com
  </code></pre>
  
  <p>and</p>
  
  <pre><code>IIS:\SslBindings\0.0.0.0!443
  </code></pre>
  
  <p>The Certificate has a subject of <em>cloud.domain.com</em>, and multiple SAN attributes, e.g. for **.domain.com*, <em>domain.com</em>, **.seconddomain.com*, <em>seconddomain.com</em>, <em>cloud.domain.com</em></p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Right now I'm using this approach, which does work:</p>
  
  <pre><code>$guid = [guid]::NewGuid().ToString("B")
  netsh http add sslcert hostnameport=$Name.domain.com:443 certhash=b58e54ca68c94f93c134c5da00a388ab0642a648 certstorename=MY appid="$guid"
  </code></pre>
  
  <p>I'm still interested however in a solution without <code>netsh</code> / <code>appcmd</code></p>