使用PowerShell获取带密码PFX文件的证书Thumbprint

26

我正在尝试使用以下代码获取受密码保护的pfx文件的Thumbprint:

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 $_;
    }
}

当我运行它时,出现了这个错误:
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

有人能帮我解决这个问题吗?

谢谢大家。:-)

7个回答

64

1
微软示例: PS C:> Get-PfxCertificate -FilePath "C:\windows\system32\Test.pfx" - niklasolsn
6
Get-PfxCertificate命令没有密码参数。如果需要在非交互模式下导入证书,请参考kyorilys的答案。 - Der_Meister
在2023年,它具有密码参数:(Get-PfxCertificate -FilePath $certFilePath -Password $certPassword).Thumbprint。密码:ConvertTo-SecureString 'myPassword' -AsPlainText -Force - Endy Tjahjono

23

你可以做到这一点

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

请记得设置这两个变量:$CertificatePath 和 $sSecStrPassword。

更新:

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

2
在新版本中,您应该使用以下代码: $certificateObject = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($CertificatePath, $sSecStrPassword) - Dinirex
我遇到了一个问题:“使用3个参数调用“Import”时出现异常:“指定的网络密码不正确。” 我正在使用你的脚本。有什么想法吗? - Imran Khan
可能是由于新版本的原因,或者像描述中所说,密码不正确,您需要设置$sSecStrPassword。 - kyorilys
1
给其他尝试的人注意:显然证书路径需要是绝对路径。我使用Resolve-Path解析相对路径,它完美地工作了。 - moronator

4
PowerShell错误信息是正确的。没有接受两个参数的重载。根据您使用的参数,我认为您想要需要第三个参数 - 枚举类型 X509KeyStorageFlags 的重载。
$certificateObject.Import($CertificatePath, $sSecStrPassword, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::DefaultKeySet)

3

以下是我用于在Windows PowerShell 5.1中读取证书文件中指纹信息的代码,无需导入文件:

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

有关Get-PfxData的更多信息,请参见此处: https://learn.microsoft.com/en-us/powershell/module/pkiclient/get-pfxdata


1

感谢这个答案:有一个命令行实用程序可以提取证书的指纹吗? 我能够编写出以下一行代码,它非常好用:

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

如果PFX文件被密码保护,
    $thumbprint = (certutil -split -p the_secret_password_to_my_pfx -dump .\cert.pfx | findstr /c:"Cert Hash(sha1)").Substring(17)[-1]

从技术上讲,它不是纯PowerShell,因为它调用了certutil.exe,但这应该在每个Windows系统上都存在,所以它可以工作。


1

1

如果您在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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接