powershell - 使用powershell获取证书信息

标签 powershell ssl powershell-core

我的想法是拥有一个适用于(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/

相关文章:

windows - 有没有一种简单的方法可以检查系统上是否启用了 CredSSP?

powershell - 当将Select-Object与First参数一起使用时,OutVariable包含不正确的数据

powershell - Powershell允许取消启动过程-wait

powershell - 递归搜索所有文件夹并非递归查找文件最多的文件夹

powershell - 卸载组件

Powershell:在第一个函数参数已经定义时隐藏第二个函数参数

java - Tomcat 9 keystore 密码无效

ssl - 让 webservice 使用 SSL

通过 SSL https 连接的 Laravel IO Socket 和 redis

powershell - 如何更改 PowerShell 提示颜色?