Импорт-PfxCertificate no-ops, но без ошибок или чего-либо еще - PullRequest
0 голосов
/ 21 мая 2019

Я пытаюсь импортировать файл PFX в локальное хранилище сертификатов. Однако Import-PfxCertificate просто ничего не делает. Нет возвращаемого значения, нет ошибки, ничего:

enter image description here

Я могу дважды щелкнуть файл PFX в Проводнике и импортировать его с тем же паролем, который работает. Что-то в PowerShell CmdLet не работает. Я также пробовал другие магазины, такие как Cert:\LocalMachine\My и TrustedPeople. Запуск с -Verbose -Debug ничего лишнего не показывает. Ничего в журналах событий приложений или безопасности тоже нет. Я также работаю в качестве администратора. Идеи?

1 Ответ

0 голосов
/ 22 мая 2019

Файл Pfx может иметь цепочку сертификатов.Рассматривать его как коллекцию было бы лучшим способом обработки хранилища сертификатов.См. установка цепочки сертификатов для C #, основанного на этом;

[string] $certPath = '.\test.pfx';
[string] $certPass = 'MyPassword';

# Create a collection object and populate it using the PFX file
$collection = [System.Security.Cryptography.X509Certificates.X509Certificate2Collection]::new();
$collection.Import($certPath, $certPass,  [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::PersistKeySet);

try {
    # Open the Store My/Personal
    $store = [System.Security.Cryptography.X509Certificates.X509Store]::new('My');
    $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite);

    foreach ($cert in $collection) {
        Write-Host ("Subject is: '{0}'" -f  $cert.Subject  )
        Write-Host ("Issuer is:  '{0}'" -f  $cert.Issuer  )

        # Import the certificate into an X509Store object
        # source https://support.microsoft.com/en-au/help/950090/installing-a-pfx-file-using-x509certificate-from-a-standard-net-applic

        if ($cert.Thumbprint -in @($store.Certificates | % { $_.Thumbprint } )) {
            Write-Warning "Certificate is already in the store"
            # Force the removal of the certificate so we have no conflicts, not required if this is the first install    
            $store.Remove($cert)
        }
        # Add in the certificate 
        $store.Add($cert);
    }
} finally {
    if($store) {
        # Dispose of the store once we are done
        $store.Dispose()
    }
}
...