2017-01-11 73 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)}