【问题标题】:Detecting SSL certificates due for expiry检测到期的 SSL 证书
【发布时间】:2017-01-11 22:00:34
【问题描述】:

我有 *.cer 文件,这些文件是驻留在本地磁盘上的项目的一部分。它们没有安装在我的系统中。

说证书的位置是:C:\Users\wrick\Source\Repos\MyProject\Src\Certificates\

现在我想运行一个 PowerShell 脚本,它会为我提供将在 90 天内到期的 *.cer 文件列表。

这是我写的:

Get-ChildItem -Recurse -Path C:\Users\wrick\Source\Repos\MyProject\Src\Certificates\ |
    Select-Object -Property PSChildName, Subject,
        @{n=’ExpireInDays’;e={($_.notafter – (Get-Date)).Days}} |
    Where-Object {$_.ExpireInDays -lt 90 -and $_.Extension -like "*.cer"}

我添加了$_.Extension -like "*.cer" 部分,因为 cert 目录也有一些其他脚本文件。 当我运行这个脚本时,我没有得到任何输出。

【问题讨论】:

  • 我认为在 Cert:\ 上执行 gci 时,您只会获得 notafter 属性。不在.cer 文件上。

标签: powershell ssl


【解决方案1】:

您仍然可以在 powershell 中执行此操作,只需进入 .Net 框架并使用 System.Security.Cryptography.X509Certificates.X509Certificate 类。

#Filter for *.cer files as early as possible
Get-ChildItem -Recurse -Path C:\Users\wrick\Source\Repos\MyProject\Src\Certificates\ -Filter *.cer |
#grab the full file path, read in the cert, get the expiration date and convert to a datetime object
select FullName,@{name='ExpirationDate';e={[DateTime]([System.Security.Cryptography.X509Certificates.X509Certificate2]::CreateFromCertFile($_.FullName).GetExpirationDateString())}} |
#filter for those that expire within 90 days
Where-Object {$_.ExpirationDate -lt [DateTime]::Today.AddDays(90)}

【讨论】:

    猜你喜欢
    • 2012-03-29
    • 1970-01-01
    • 2019-11-08
    • 2014-10-14
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    相关资源
    最近更新 更多