我的想法是拥有一个适用于(windows 和 ubuntu)的 powershell 代码,只需很少的更改(比如 get-childitem 中的路径),所以我在 ubuntu 上尝试了以下脚本,但在 windows 和它向我显示以下错误
openssl.exe : Can't open [Subject]
At line:5 char:10
- $var = ((& C:\OpenSSL-Win64\bin\openssl.exe x509 -in $File -dates -no ...
这是我写的代码:
$files = get-childitem Cert:\LocalMachine\My
foreach ($File in $files)
{
$var = ((& C:\OpenSSL-Win64\bin\openssl.exe x509 -in $File -dates -noout) -
match 'notAfter')
Write-Host $var
}
另注:openssl使用什么语法来获取证书名称
最佳答案
openssl x509 -in
需要一个文件路径作为参数,而不是 [X509Certificate]
对象的自定义格式输出。
你可以做的是 re-encode the certificate in PEM format (base64) 并将其通过管道传输到 openssl
:
if($IsWindows){
foreach($cert in Get-ChildItem cert:\LocalMachine\My\) {
# Write BEGIN line
$certStrings = @('-----BEGIN CERTIFICATE-----')
# Export cert data
$certData = $cert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert)
# Convert to Base64 + append
$certStrings += [Convert]::ToBase64String($certData, [System.Base64FormattingOptions]::InsertLineBreaks)
# Write END line
$certStrings += '-----END CERTIFICATE-----'
# Pass off to openssl.exe
$NotAfter = @(($certStrings -join [System.Environment]::NewLine) | .\path\to\openssl.exe x509 -text -noout -dates) -match 'notAfter'
Write-Host $NotAfter
}
}
else {
foreach($cert in Get-ChildItem cert:\LocalMachine\My\) {
$notAfter = @(& path\to\openssl.exe x509 -in $cert -dates -noout) -match 'notAfter'
Write-Host $notAfter
}
}
关于powershell - 使用powershell获取证书信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56074535/