【问题标题】:PowerShell Get Certificate Thumbprint with Password PFX FilePowerShell 使用密码 PFX 文件获取证书指纹
【发布时间】:2015-01-08 17:42:03
【问题描述】:

我正在尝试使用此代码获取受密码保护的 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 $_;
    }
}

当我运行它时,我得到了这个错误:

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

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

谢谢大家。 :-)

【问题讨论】:

    标签: powershell passwords x509certificate2


    【解决方案1】:

    PowerShell 错误消息是正确的。没有采用两个参数的重载。根据您使用的参数,我认为您想要 overload that requires a third parameter - 一个枚举 - X509KeyStorageFlags 例如

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

    【讨论】:

      【解决方案2】:

      根据this SuperUser response,在PS 3.0中有Get-PfxCertificate command可以做到这一点:

       Get-PfxCertificate -FilePath Certificate.pfx 
      

      【讨论】:

      • 来自 Microsoft 的示例:PS C:\> Get-PfxCertificate -FilePath "C:\windows\system32\Test.pfx"
      • Get-PfxCertificate 没有密码参数。如果您需要在非交互模式下导入证书,请参阅 kyorilys 的答案。
      【解决方案3】:

      你可以这样做

      $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)
      【解决方案4】:

      仅供参考,看起来 Get-PfxCertificate 将添加在 powershell 6.0 中传递密码的功能。

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

      【讨论】:

        【解决方案5】:

        感谢这个答案:Is there a command line utility to extract the certificate thumbprint? 我能够制定出以下效果很好的单线:

            $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 系统上,所以它可以工作。

        【讨论】:

          【解决方案6】:

          这是我用来读取文件中证书的指纹而不在 Windows PowerShell 5.1 上导入文件的方法:

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

          有关 Get-PfxData 的更多信息可在此处找到: https://docs.microsoft.com/en-us/powershell/module/pkiclient/get-pfxdata

          【讨论】:

            【解决方案7】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2016-11-03
              • 2020-10-27
              • 2014-04-19
              • 2017-12-19
              • 2015-06-30
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多