首页 > 解决方案 > windows cmd/Powershell如何在没有密码提示的情况下从私钥pfx文件中导出公钥?

问题描述

从 pfx 导出证书”可以无缝进行,无需用户输入密码。

Powershell 6.1+ commandlet 允许-password切换Get-PfxCertificate

对于早期版本,可能的解决方案是:

powershell (Get-PfxCertificate -FilePath Private.pfx).GetPublicKey()

,用户必须在命令提示符下输入密码,否则会出错:
Get-PfxCertificate : The specified network password is not correct.

密码必须在提示时输入;不接受复制/粘贴。也没有为命令提供密码的开关。

标签: cmdpasswordsexportpublic-keypfx

解决方案


Get-Pfxdata做同样的工作,允许在脚本中处理密码,而无需用户干预受密码保护的 pfx 文件:

set /p pwd="Enter password: "
pwdstring
set pwd=convertto-securestring \"%pwd%\" -asplaintext -force
powershell Export-Certificate -Cert ((Get-PfxData -FilePath Private.pfx -password (%pwd%)).EndEntityCertificates[0]) -FilePath Public.cer

在 Win 10 CMD 上测试


推荐阅读