2014-11-12 2 views
4

Я пытаюсь получить отпечаток, защищенный паролем файл PFX, используя этот код:PowerShell Получить сертификат Отпечаток с паролем 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 

Может кто-нибудь, пожалуйста, помогите мне разобраться с этим?

Спасибо всем. :-)

ответ

4

Сообщение об ошибке PowerShell является правильным. Нет перегрузок, которые принимают два параметра. На основе параметров, которые вы используете, я думаю, вы хотите, чтобы overload that requires a third parameter - перечисление - X509KeyStorageFlags, например.

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

Согласно this SuperUser response, в PS 3.0 есть Get-PfxCertificate command сделать:

Get-PfxCertificate -FilePath Certificate.pfx 
+1

Пример от Microsoft: PS C: \> Get- PfxCertificate -FilePath "C: \ windows \ system32 \ Test.pfx" – niklasolsn

+1

Get-PfxCertificate не имеет пароля. См. Ответ kyorilys, если вам нужно импортировать сертификат в неинтерактивный режим. –

6

Вы можете сделать это

$certificateObject = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 
$certificateObject.Import($CertificatePath, $sSecStrPassword, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::DefaultKeySet) 
return $certificateObject.Thumbprint 
Смежные вопросы