我是一个强大的Powershell新手,这是我的第一篇文章,因此,我将因缺乏知识和/或错误地使用术语而向您道歉...我已经花费了数小时来搜索和尝试各种代码片段以创建一个脚本,该脚本可用于替换IIS(7.5及更高版本)Web服务器上的数百个SSL证书。到目前为止,我发现SSL绑定显然存储在[HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ services \ HTTP \ Parameters \ SslBindingInfo]中。以下powershell脚本允许我检索服务器上存在的所有IIS SSL绑定,而CName的简单比较允许我找到与我们要更新的证书匹配的特定绑定。我还发现,我可以使用其他指纹在上面位置下方的注册表项中手动改写SslCertHash值,它会更改IIS中的证书绑定。因此,在powershell中似乎应该有一种非常简单的方法,即在适用的绑定中用新的指纹替换当前的指纹,而不必检索IP地址,端口和/或siteID(正在运行的脚本已经使我处于绑定状态我需要更改的实例)。我知道有一些绕行方式可以更改绑定,但是我对powershell的有限了解表明应该存在一个更直接的命令来完成此操作。那只是一厢情愿吗?
到目前为止,我的脚本是
Import-Module -Name WebAdministration
$SearchCName = '*myhost.domain.com*'
$NewCertThumbprint = '1A2B3C4D5E6F7G8H9I0J1K2L3M4N5O6P7Q8R9S0T'
Get-ChildItem -Path IIS:SSLBindings | ForEach-Object -Process {
if ($_.Sites)
{
$certificate = Get-ChildItem -Path CERT:LocalMachine/My |
Where-Object -Property Thumbprint -EQ -Value $_.Thumbprint
[PsCustomObject]@{
Sites = $_.Sites.Value
CertificateDnsNameList = $certificate.DnsNameList
CertificateNotAfter = $certificate.NotAfter
CertificateThumbprint = $certificate.Thumbprint
CertificateSubject = $certificate.Subject
CertificateIssuer = $certificate.Issuer
CertificateTPfromSSLBinding = $_.Thumbprint
CertificateBindingIPAddr = $_.IPAddress
CertificatePort = $_.Port
}
if ($certificate.Subject -like $SearchCName)
{ Write-Output 'True' }
Else
{ Write-Output 'False' }
}
}
Sample output:
Sites : testsite1
CertificateDnsNameList : {}
CertificateNotAfter : 2/16/2021 1:41:55 PM
CertificateThumbprint : 6DA6A25F5C756D710D033D1146A3097EE0E9F430
CertificateSubject : CN=testsite1.domain.com, OU=IT Security, O=My
Company, L=MyCity, S=MyState, C=US
CertificateIssuer : CN=My Company Internal CA, OU=PKI, O=My Company,
C=US
CertificateTPfromSSLBinding : 6DA6A25F5C756D710D033D1146A3097EE0E9F430
CertificateBindingIPAddr : 10.39.205.19
CertificatePort : 443
False
Sites : testsite2
CertificateDnsNameList : {}
CertificateNotAfter : 1/2/2022 4:39:12 PM
CertificateThumbprint : 6D2DC913256CCADBD3983773CAC20440D918F091
CertificateSubject : CN=testsite2.domain.com, OU=IT Security, O=My
Company, L=MyCity, S=MyState, C=US
CertificateIssuer : CN=My Company Internal CA, OU=PKI, O=My Company,
C=US
CertificateTPfromSSLBinding : 6D2DC913256CCADBD3983773CAC20440D918F091
CertificateBindingIPAddr : 10.39.205.24
CertificatePort : 443
False
我的Powershell版本: 重大次要版本修订
5 0 10586 117
谢谢, 兰迪