代码之家  ›  专栏  ›  技术社区  ›  Jez

在powershell中设置IIS绑定证书时发出

  •  13
  • Jez  · 技术社区  · 6 年前

    我正在编写一个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 代码和“我的”看起来不错。我唯一能想到的是,它可能在当前用户证书存储中查找,而不是在本地计算机证书存储中查找,这是所需证书所在的位置。有人能帮我解释一下为什么会发生这个错误吗?

    1 回复  |  直到 6 年前
        1
  •  5
  •   Jez    6 年前

    原来我也有同样的问题 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
    }