【问题标题】:Import-PfxCertificate no-ops, but no error or anythingImport-PfxCertificate 无操作,但没有错误或任何东西
【发布时间】:2019-10-08 04:35:13
【问题描述】:

我正在尝试将 PFX 文件导入本地证书存储。然而,Import-PfxCertificate 什么都不做。没有返回值,没有错误,什么都没有:

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

【问题讨论】:

  • 想通了。这是因为 PFX 没有私钥。似乎安装在 Service Fabric 群集上的证书不可导出,因此当您与 Docker 容器共享它们时,它们的私钥会被剥离。
  • 我遇到了同样的问题。这是由于链中的证书存储在多个位置(重复)引起的。我导出了证书及其链,删除了所有重复项,导入,然后运行我的脚本来安装新证书。然后我得到了输出。好像没有输出就是错误。

标签: powershell certificate windows-server-2016 pfx


【解决方案1】:

Pfx 文件可能有一个证书链。将其视为一个集合将是处理证书存储的更好方法。请参阅 installing cert chain 了解基于此的 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()
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 2021-10-16
    • 1970-01-01
    相关资源
    最近更新 更多