简体   繁体   English

PowerShell 使用密码 PFX 文件获取证书指纹

[英]PowerShell Get Certificate Thumbprint with Password PFX File

I'm trying to get the thumbprint of a password protected pfx file using this code:我正在尝试使用以下代码获取受密码保护的 pfx 文件的指纹:

function Get-CertificateThumbprint {
    # 
    # This will return a certificate thumbprint, null if the file isn't found or throw an exception.
    #

    param (
        [parameter(Mandatory = $true)][string] $CertificatePath,
        [parameter(Mandatory = $false)][string] $CertificatePassword
    )

    try {
        if (!(Test-Path $CertificatePath)) {
            return $null;
        }

        if ($CertificatePassword) {
            $sSecStrPassword = ConvertTo-SecureString -String $CertificatePassword -Force –AsPlainText
        }

        $certificateObject = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
        $certificateObject.Import($CertificatePath, $sSecStrPassword);

        return $certificateObject.Thumbprint
    } catch [Exception] {
        # 
        # Catch accounts already added.
        throw $_;
    }
}

When I run it, I get this error:当我运行它时,我收到此错误:

Cannot find an overload for "Import" and the argument count: "2".
At C:\temp\test.ps1:36 char:9
+         $certificateObject.Import($CertificatePath, $sSecStrPassword);
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

Can someone please help me sort this out?有人可以帮我解决这个问题吗?

Thanks All.谢谢大家。 :-) :-)

根据这个 SuperUser response ,在 PS 3.0 中有Get-PfxCertificate 命令来做到这一点:

 Get-PfxCertificate -FilePath Certificate.pfx 

You can do this你可以这样做

$certificateObject = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$certificateObject.Import($CertificatePath, $sSecStrPassword, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::DefaultKeySet)
return $certificateObject.Thumbprint

Remember to set this two variable: $CertificatePath and $sSecStrPassword记得设置这两个变量:$CertificatePath 和 $sSecStrPassword

The PowerShell error message is right. PowerShell 错误消息是正确的。 There are no overloads that take two parameters.没有带有两个参数的重载。 Based on the parameters you are using I think you want theoverload that requires a third parameter - an enum - X509KeyStorageFlags eg根据您使用的参数,我认为您需要需要第三个参数重载- 枚举 - X509KeyStorageFlags例如

$certificateObject.Import($CertificatePath, $sSecStrPassword, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::DefaultKeySet)

Here is what I have used to read the thumbprint of a certificate in a file without importing the file on Windows PowerShell 5.1:这是我用来读取文件中证书指纹而不在 Windows PowerShell 5.1 上导入文件的方法:

$Thumbprint = (Get-PfxData -Password $MyPFXCertificatePwdSecureString -FilePath $CertificateFilePath).EndEntityCertificates.Thumbprint

More information about Get-PfxData can be found here: https://docs.microsoft.com/en-us/powershell/module/pkiclient/get-pfxdata有关 Get-PfxData 的更多信息,请访问: https ://docs.microsoft.com/en-us/powershell/module/pkiclient/get-pfxdata

FYI, looks like Get-PfxCertificate will add the ability to pass a password in powershell 6.0.仅供参考,看起来 Get-PfxCertificate 将添加在 powershell 6.0 中传递密码的功能。

https://github.com/PowerShell/PowerShell-Docs/issues/2150 https://github.com/PowerShell/PowerShell-Docs/issues/2150

Thanks to this answer: Is there a command line utility to extract the certificate thumbprint?感谢这个答案: 是否有命令行实用程序来提取证书指纹? I was able to work out the following one-liner that works great:我能够计算出以下效果很好的单线:

    $thumbprint = (certutil -split -dump .\cert.pfx | findstr /c:"Cert Hash(sha1)").Substring(17)[-1]

If the PFX is password protected,如果 PFX 受密码保护,

    $thumbprint = (certutil -split -p the_secret_password_to_my_pfx -dump .\cert.pfx | findstr /c:"Cert Hash(sha1)").Substring(17)[-1]

Tehcnically, it's not pure powershell, as it invokes certutil.exe, but that should be on every Windows system, so it works.从技术上讲,它不是纯粹的 powershell,因为它调用了 certutil.exe,但它应该在每个 Windows 系统上,所以它可以工作。

If you get path error in powershell, use below script:如果您在 powershell 中遇到路径错误,请使用以下脚本:

$FilePath = "c:\a\"
$FileName = "mycert"
$FileType = ".pfx"
$certificateObject = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$certificateObject.Import($FilePath+$FileName+$FileType, $sSecStrPassword, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::DefaultKeySet)
return $certificateObject.Thumbprint

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

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