【问题标题】:Issue when setting IIS binding certificate in Powershell在 Powershell 中设置 IIS 绑定证书时出现问题
【发布时间】:2018-11-16 18:45:15
【问题描述】:

我正在编写一个 Powershell 脚本来检查我的 IIS 绑定列表,找到任何具有特定“旧”证书指纹的内容,并将其证书替换为具有“新”指纹的证书。这样我就可以为许多绑定更新证书,因为我们对很多站点使用相同的证书,所以我们需要将所有具有旧证书的绑定更新为新证书。这是我想出的:

##### EDIT THESE VARIABLES #####
$SiteName = "movc-website-www"
$OldCertThumbprint = "‎76 ae 0b 2e b9 f7 45 ce 27 c4 02 6e 90 66 62 93 69 d7 5e 4c"
$ReplacementCertThumbprint = "‎7f fa 9f f3 90 b8 a2 d8 4c 98 51 47 a5 64 1d 90 f6 2f ca 73"

##### FUNCTIONS #####
Function ReplaceWebsiteBinding {
    Param(
        [string] $SiteName,
        [string] $OldCertThumbprint,
        [string] $ReplacementCertThumbprint
    );

    Import-Module WebAdministration;

    $ReplacedCount = 0

    $IISBindings = (Get-ItemProperty -Path "IIS:\Sites\$SiteName" -Name Bindings)
    for ($i=0; $i -lt ($IISBindings.Collection).Length; $i++) {
        if (($IISBindings.Collection[$i]).certificateHash -eq $OldCertThumbprint) {
            ($IISBindings.Collection[$i]).RebindSslCertificate($ReplacementCertThumbprint, "My")
            $ReplacedCount++
        }
    }

    Return $ReplacedCount
}

##### MAIN PROGRAM #####
$OldCertThumbprint = $OldCertThumbprint.Replace(" ", "").ToUpper()
$ReplacementCertThumbprint = $ReplacementCertThumbprint.Replace(" ", "").ToUpper()

# Check that cert with given thumbprints exist
$FoundCert = Get-ChildItem -Path Cert:\LocalMachine\My |
    Where-Object { $_.Thumbprint -eq $OldCertThumbprint } |
    Select-Object -ExpandProperty Thumbprint
if (!$FoundCert) {
    Write-Host "Old cert with thumbprint $OldCertThumbprint not found!"
    Exit
}

$FoundCert = Get-ChildItem -Path Cert:\LocalMachine\My |
    Where-Object { $_.Thumbprint -eq $ReplacementCertThumbprint } |
    Select-Object -ExpandProperty Thumbprint

if (!$FoundCert) {
    Write-Host "Replacement cert with thumbprint $ReplacementCertThumbprint not found!"
    Exit
}

# Associate new cert with bindings that have old cert
$ReplacedCount = ReplaceWebsiteBinding $SiteName $OldCertThumbprint $ReplacementCertThumbprint

Write-Host "Replaced $ReplacedCount binding(s)."

问题在于这不起作用,因为调用 .RebindSslCertificate(...) 的行给了我以下 Powershell 错误:

Value does not fall within the expected range.
At (...)
+             ($IISBindings.Collection[$i]).RebindSslCertificate($Repla ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException

真的不是最有用的错误,我不知道为什么我会得到它。这些论点在我看来还可以;指纹是由Get-ChildItem 代码找到的,“我的”看起来不错。我唯一能想到的是它可能正在查看当前用户证书存储而不是本地机器证书存储,这是所需证书所在的位置。谁能帮我解释为什么会发生这个错误?

【问题讨论】:

  • 您的代码看起来正确。您是否以管理员身份运行?如果您只运行以下代码,是否会显示旧的和替换的指纹/主题? "$FoundCert = Get-ChildItem -Path Cert:\LocalMachine\My"
  • 是的,我以管理员身份运行。是的,它们会显示出来。

标签: powershell ssl iis certificate


【解决方案1】:

原来我遇到了与here 所述相同的问题(RebindSslCertificate 证书与删除它然后调用AddSslCertificate 相同)。当出于某种愚蠢的原因从 Windows 中的证书对话框复制指纹时,它会在字符串的开头插入一个零宽度的 LTR 字符,因此我的指纹无效。我在脚本开头添加了一个检查以防止它:

if ($OldCertThumbprint -match "[\W-[\ ]]") {
    Write-Host "Old cert thumbprint contains non-word characters, maybe a zero-width LTR Unicode character at the beginning.  You almost certainly don't want this!  Aborting!"
    Exit
}
if ($ReplacementCertThumbprint -match "[\W-[\ ]]") {
    Write-Host "Replacement cert thumbprint contains non-word characters, maybe a zero-width LTR Unicode character at the beginning.  You almost certainly don't want this!  Aborting!"
    Exit
}

【讨论】:

  • 要进一步进行防弹指纹验证,$thumbprint -match '^[a-fA-F0-9]{40}$'。 (假设 sha1 哈希)
猜你喜欢
  • 2019-05-01
  • 2015-11-30
  • 2023-03-13
  • 1970-01-01
  • 2018-11-07
  • 1970-01-01
  • 2011-07-16
  • 1970-01-01
  • 2013-10-05
相关资源
最近更新 更多