繁体   English   中英

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

[英]Powershell - Set SSL Certificate on https Binding

我正在尝试使用 PowerShell 在 IIS 站点上为自签名\/本地证书设置 SSL 证书。

我创建证书:

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

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

您可以使用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”相同的错误,我发现需要一个额外的步骤:

在绑定到添加到 IIS 网站绑定之前,SSL 证书需要在证书存储中。 这可以使用以下命令在 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM