如何使用 PowerShell 获取域的最新 SSL 证书?

How can I get the most recent SSL certificate for a domain using PowerShell?

我正在尝试在虚拟主机证书存储区中为给定域查找最新的证书(例如 www.example.com)

很容易找到任意数量的匹配证书,但我如何才能找到最新的证书,并按到期日期(最远的未来)排序?

我现有的代码是:

(Get-ChildItem -Path cert:\LocalMachine\WebHosting 
   | Where-Object {$_.Subject -match "example.com"}).Thumbprint;

然而,这 returns 两个证书有时与以前的证书(续订之前)一样必须在证书存储中保留一小段时间。

您可以尝试按 属性 notafter

排序

查看所有属性:

(Get-ChildItem -Path cert:\LocalMachine\WebHosting | Where-Object {$_.Subject -match "example.com"}) | fl *

排序方式 notAfter 属性 :

(Get-ChildItem -Path cert:\LocalMachine\ca | Where-Object {$_.Subject -match ".*microsoft.*"}) | Sort-Object -Property NotAfter -Descending

似乎按 NotAfter 排序会导致在使用 Letsencrypt 时出现问题,而您仍然有 1 年的有效证书在 Letsencrypt 证书后到期。

通过使用 NotBefore 进行排序使其工作...不过还没有进行太多测试。