Export-PfxCertificate:无法导出不可导出的私钥

问题描述 投票:4回答:4

我试图导出我的自签名证书,以便我可以将其导入我的开发环境中的其他服务器(将使用“真正的”生产证书),但它会抛出以下错误:

Export-PfxCertificate:无法导出不可导出的私钥

要求是我需要导出证书并“允许导出私钥”,但我很好奇我错过了什么。我的PowerShell如下:

$pwd = ConvertTo-SecureString -String ‘1234’ -Force -AsPlainText
$path = 'cert:\localMachine\my\' + '1E7439053EE57AEE6EA0E1F3CDF5DB4234B6731E' 
Export-PfxCertificate -cert $path -FilePath c:\Certificates\cert.pfx -Password $pwd
powershell ssl-certificate
4个回答
10
投票

问题不在于powershell代码。问题出在证书上。

首次导入或创建证书时,必须将私钥标记为可导出,以便您能够导出私钥。

您收到的错误消息表明私钥不能在您尝试使用的证书上导出。

Example Issue


2
投票

我做了一个快速搜索,你可以使用certutil或更好的可能是来自http://community.idera.com/powershell/powertips/b/tips/posts/exporting-certificate-with-private-key的解决方案。

该帖子的相关代码已粘贴在下方。 100%归属于该页面的作者。

dir cert:\currentuser\my | 
Where-Object { $_.hasPrivateKey } | 
Foreach-Object { [system.IO.file]::WriteAllBytes(
"$home\$($_.thumbprint).pfx", 
($_.Export('PFX', 'secret')) ) }

1
投票

检查我的代码如下。

#Ask for the Name 
$name = Read-Host "Certificate Name "

# Check if the Path exists
$Path = "D:\Provisioning\certmgmt\$name.txt"
$TestPath = Test-Path $Path
if ($TestPath -ne "true")
{
    Write-Host "The Path $Path do not exist" -ForegroundColor Red
    Pause
    exit
}

# Import the certificate
$result = Import-Certificate -FilePath $Path -CertStoreLocation "Cert:\LocalMachine\My" 

# Get the serialnumber of the certificate
$Thumbprint = $result.Thumbprint

# Set the FriendlyName
(Get-ChildItem -Path Cert:\LocalMachine\My\$Thumbprint).FriendlyName = $name  

# Export the Certificate
$answer = Read-Host "Export Certificate? (Y/N)"

if ($answer -eq "N" -or $answer -eq "n")
{
    exit
}


    try
    {
       $mypwd = ConvertTo-SecureString -String "password" -Force -AsPlainText
       Get-ChildItem -Path cert:\localMachine\my\$Thumbprint | Export-PfxCertificate -FilePath C:\$name.pfx -Password $mypwd
    }
    catch
    {
      Write-Host $Error -ForegroundColor Red
      pause
      exit
    }

    Write-Host "Export the Certifikate was successful" -ForegroundColor Green

0
投票

可能为时已晚,但您是否尝试以管理员身份运行PowerShell脚本? (如果您可以从mmc控制台导出私钥,Export-PfxCertificate也会将其导出。)

© www.soinside.com 2019 - 2024. All rights reserved.