Import-PfxCertificate无操作,但没有错误或任何错误

时间:2019-05-21 18:06:49

标签: powershell certificate windows-server-2016 pfx

我正在尝试将PFX文件导入本地证书存储。但是,Import-PfxCertificate根本不执行任何操作。没有返回值,没有错误,什么都没有:

enter image description here

我可以双击资源管理器中的PFX文件,然后使用相同的密码将其导入,这是可行的。关于PowerShell CmdLet的某些信息不起作用。我还尝试了其他商店,例如Cert:\LocalMachine\MyTrustedPeople。使用-Verbose -Debug运行它不会显示任何多余的信息。应用程序或安全事件日志中也没有任何内容。我也以管理员身份运行。想法?

1 个答案:

答案 0 :(得分:0)

Pfx文件可能具有证书链。将其视为集合将是处理证书存储的更好方法。有关基于C#的C#,请参见installing cert chain

[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()
    }
}