Powershell 导出PfxCertificate:无法导出不可导出的私钥

Powershell 导出PfxCertificate:无法导出不可导出的私钥,powershell,ssl-certificate,Powershell,Ssl Certificate,我正在尝试导出我的自签名证书,以便将其导入到我的开发环境中的其他服务器(将使用“真实”证书进行生产),但它会引发以下错误: 导出PfxCertificate:无法导出不可导出的私钥 要求是我需要导出证书和“允许导出私钥”,但我很好奇我遗漏了什么。我的PowerShell如下所示: $pwd = ConvertTo-SecureString -String ‘1234’ -Force -AsPlainText $path = 'cert:\localMachine\my\' + '1E743905

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

导出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

我做了一个快速搜索,您可以使用
certutil
或更好的方法,这可能是来自的解决方案

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

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

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

首次导入或创建证书时,必须将私钥标记为可导出,才能导出私钥

您收到的错误消息表明您尝试使用的证书上无法导出私钥

检查下面的我的代码

#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

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

我知道这是一个老问题,但我想发布我的解决方案,因为我遇到了同样的问题。 我也在尝试导出我的PFX文件时遇到可怕的导出PFX证书:无法导出不可导出的私钥。 在Windows计算机上加载代码签名证书后,问题开始出现。 当我去导出它时,导出到PFX选项是灰色的,没有进一步的解释。然后,我遵循了这里列出的许多说明,包括Powershell导出PfxCertificate。这些都不起作用。 最后,我回到了我的证书提供商GoDaddy,他们告诉我,在我的原始证书签名请求(CSR)中,我没有选中“使私钥可导出”框。 GoDaddy慷慨地、免费地允许我提交一份新的CSR(选中该选项),以“重新输入”我现有的证书。几个小时后,我的新证书就颁发了。我在我的机器上安装了它,可以直接从Windows MMC导出(无需PowerShell) 我已经包括了这个框的屏幕截图,在创建CSR时必须选中它(在不同的平台上可能看起来不同)


检查这篇旧的SO帖子:不确定是否有现成的方法可以使用powershell完成,但也许可以与mimikatz或越狱结合使用,您可以在一个循环中批量完成此操作我有一个脚本,用于批量导出证书。。。不确定这是否有用,但值得一试导出密钥不需要管理员身份,但需要密钥可用才能导出密钥。如果以用户身份启动脚本,则只能从\LocalMachine\My导出公钥。此外,Export-PfxCertificate返回错误“无法导出不可导出的私钥”。但是,如果我从提升的命令提示符启动PS,则不会出现错误,并且PFX文件包含pub和priv key。如果您可以在
certlm.msc
中手动导出证书,因为它被标记为可导出,但在PowerShell中仍然会出现此错误,那么这就是解决方案。例如,如果未提升,则无法导出IIS证书。示例链接已断开。