简体   繁体   English

从文件系统中的证书获取证书到期日期

[英]Get certificate expiration date from certificate in file system

I'm using Windows PowerShell script to compare the expiration date of 2 certificates, 1 stored in the certificate store and 1 stored in my file system (C:\inetpub).我正在使用 Windows PowerShell 脚本来比较 2 个证书的到期日期,其中 1 个证书存储在证书存储中,1 个证书存储在我的文件系统 (C:\inetpub) 中。

However, when i test the output i can't get the expiration date from the certificate that's not in the certificate store.但是,当我测试 output 时,我无法从不在证书存储中的证书中获取到期日期。 It just shows a blank space when i use Write-Output to check it.当我使用 Write-Output 检查它时,它只显示一个空格。 Maybe my syntax is wrong or maybe its not possible?也许我的语法错误或者不可能?

This is what I'm currently trying:这是我目前正在尝试的:

$certName = "websitename.com"
$exportPath = "C:\"
$filePathCRT = $exportPath + $certName + ".crt"
$certSource = "CN=" + $certName

$srcCertFile = Get-ChildItem Cert:\LocalMachine\WebHosting | where{$_.Subject -eq $certSource}
if (!$srcCertFile)
{
    Exit
}

$desCertFile = Get-ChildItem Cert:\>C: -Path $filePathCRT
if (!$desCertFile)
{
    Write-Output "cannot get cert file"
}

if ($srcCertFile.NotAfter -ne $desCertFile.NotAfter)
{
    Write-Output "certs not the same"
    Write-Output $desCertFile.NotAfter
}

Does the certificate have to be in the certificate store to get its expiration date?证书是否必须在证书存储中才能获得其到期日期?

you can do:你可以做:

$path = [path]
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($path)
$cert.NotAfter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM