首页 > 解决方案 > 如何为网站绑定选择新安装的 SSL 证书

问题描述

我正在使用 cert util 命令添加 ssl 证书,但是在添加证书后,如何使用 power shell 为所有托管的网站选择新证书

我尝试在 powershell 下选择新安装的证书,但我收到错误 SSL 证书添加失败,错误:183 当该文件已存在时无法创建文件。有时,SSL 证书添加失败,错误:1312 指定的登录会话不存在。它可能已经被终止

$hostname="*.domain.net"
$cert = (Get-ChildItem cert:\LocalMachine\My | where-object { $_.Subject -like "*$hostname*" } | Select-Object -First 1).Thumbprint
$guid = [guid]::NewGuid().ToString("B")
netsh http add sslcert ipport="${IPAddressV2}:443" certhash=$cert certstorename=MY appid="$guid"
netsh http add sslcert ipport="${IPAddressV3}:443" certhash=$cert certstorename=MY appid="$guid"
netsh http add sslcert ipport="127.0.0.1:443" certhash=$cert certstorename=MY appid="$guid"

标签: powershell

解决方案


这是自动为端口号为 443 的所有绑定选择新安装的证书的工作脚本

Import-Module Webadministration
    IIS:
    $IPAddress = "C:\Ipaddress.txt"
    $Bindings = Get-WebBinding | Where-Object {$_.protocol -eq "https"} | Select-Object -ExpandProperty bindingInformation 
    $IPs = $Bindings | ForEach-Object { @($_ -split ':')[0]}
    $IPs = @($IPs |Sort-Object -Unique) > $IPAddress

    Import-Module Webadministration
    $Friendlyname="*.abcd.com"
    Get-ChildItem cert:\LocalMachine\My | where-object { $_.Subject -like "*$Friendlyname*" }
    $hostname = Get-Content C:\Ipaddress.txt 
    foreach ($name in $hostname) { 
    $cert = (Get-ChildItem cert:\LocalMachine\My | where-object { $_.Subject -like "*$Friendlyname*" } | Select-Object -First 1).Thumbprint
    Remove-Item -path "IIS:\SslBindings\${name}!443"
    TIMEOUT /T 5 /NOBREAK
    New-Item -path "IIS:\SslBindings\$name!443" -Thumbprint $cert}

推荐阅读