2017-01-11 58 views
0

我有* .cer文件,这些文件是驻留在本地磁盘上的项目的一部分。他们没有安装在我的系统中。检测到期的SSL证书

说证书的位置为:C:\用户\扭伤\来源\回购\ MyProject中的\ src \证书\

现在我想运行PowerShell脚本,这将使我* .CER名单在90天内到期的文件。

这是我写的:

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"部分是因为该证书目录中有一些其他脚本文件了。 当我运行这个脚本时,我没有得到任何输出。

+0

我认为在'Cert:\'上做'gci'时你只能得到'notafter'属性。不在'.cer'文件中。 – BenH

回答

3

你仍然可以做到这一点在PowerShell中,只蘸到.NET framwork和使用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)}