简体   繁体   English

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

[英]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.我正在尝试使用 PowerShell 在 IIS 站点上为自签名\/本地证书设置 SSL 证书。

I create the certificate:我创建证书:

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

You have to assign the certifcate to a specific site.您必须将证书分配给特定站点。

You can retrieve the binding information of your site using the Get-WebBinding cmdlet and set the SSL Certificate using the AddSslCertificate function:您可以使用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")

I had the same error as "Chuck D" when using the answer, I found an additional step was required:我在使用答案时遇到了与“Chuck D”相同的错误,我发现需要一个额外的步骤:

The SSL certificate needs to be in the certificate store before binding to adding to an IIS website binding.在绑定到添加到 IIS 网站绑定之前,SSL 证书需要在证书存储中。 This can be done in powershell using the following command:这可以使用以下命令在 powershell 中完成:

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

The AddSslCertificate method not available everywhere. AddSslCertificate 方法并非随处可用。 I found different solution by using Netsh.我通过使用 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.

相关问题 Powershell IIS7 Snap in将SSL证书分配给https绑定 - Powershell IIS7 Snap in Assign SSL certificate to https binding Powershell - 使用共享证书添加SSL绑定 - Powershell - Add SSL binding using shared certificate 绑定后将SSL证书分配给网站-Powershell - Assign SSL Certificate to Website After Binding - Powershell 通过 SSL 证书和 Powershell 创建 https 绑定时出现“IIS:当该文件已存在时无法创建文件。Exception.Message” - "IIS: Cannot create a file when that file already exists.Exception.Message" when creating a https binding through SSL certificate and powershell 使用PowerShell将IIS SSL证书分配给与主机标头绑定 - Assign IIS SSL Certificate to Binding with Host Header using PowerShell 如何使用PowerShell将创建的证书添加到IIS https绑定 - How to add created certificate to an IIS https binding using PowerShell IIS SSL证书绑定(获取绑定信息) - IIS SSL Certificate binding (Get binding info) 如何通过Powershell脚本设置IIS 10.0管理服务SSL证书以允许Web部署? - How do I set the IIS 10.0 Management Service SSL Certificate via a Powershell script to allow Web Deploy? appcmd为站点设置ssl证书 - appcmd set ssl certificate for site PowerShell 6. *使用SSL证书和UBUNTU进行远程处理 - PowerShell 6.* Remoting using SSL certificate and UBUNTU
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM