Powershell - 在 https 绑定上设置 SSL 证书

Powershell - Set SSL Certificate on https Binding

我正在尝试使用 PowerShell 在 IIS 站点上为自己的 signed/local 证书设置 SSL 证书。

我创建证书:

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

然后尝试设置 SSL 绑定:

get-item 
      cert:\LocalMachine\MY$newCert.Thumbprint | 
      new-item -path IIS:\SslBindings[=11=].0.0.0!443

如图所示post:http://www.iis.net/learn/manage/powershell/powershell-snap-in-configuring-ssl-with-the-iis-powershell-snap-in

也显示在这里:

我也试过:

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

无济于事,我没有在“编辑站点绑定”对话框中看到设置的 SSL 证书。

有什么想法吗?

您必须将证书分配给特定 站点。

您可以使用 Get-WebBinding cmdlet 检索站点的绑定信息,并使用 AddSslCertificate 函数设置 SSL 证书:

$siteName = 'mywebsite'
$dnsName = 'www.mywebsite.ru'

# create the ssl certificate
$newCert = New-SelfSignedCertificate -DnsName $dnsName -CertStoreLocation cert:\LocalMachine\My

# get the web binding of the site
$binding = Get-WebBinding -Name $siteName -Protocol "https"

# set the ssl certificate
$binding.AddSslCertificate($newCert.GetCertHashString(), "my")

我在使用答案时遇到了与"Chuck D"相同的错误,我发现需要额外的步骤:

SSL 证书需要在证书存储中才能绑定到 IIS 网站绑定。这可以在 powershell 中使用以下命令完成:

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

AddSslCertificate 方法并非随处可用。我使用 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

也可以设置其他参数(例如certstorename=MY)。可以在 HTTP Server API 页面上找到更多信息。