首页 > 解决方案 > Import-Pfx Cert w/Password - 部署

问题描述

我想推出带有私钥密码的证书。我知道它不安全。更安全的方式会很棒。但我想不出这样做。

$test = ConvertTo-SecureString -String "plaintextpassword" -Force -AsPlainText<code>

Import-PfxCertificate -FilePath $filelocale -CertStoreLocation Cert:\LocalMachine\My -Password $test

获取: Import-PfxCertificate:您尝试导入的 PFX 文件需要不同的密码或受保护的 Active Directory 主体中的成员身份。

如果我在下面运行它可以工作

$test = Get-Credential -UserName 'enter pwd' -Message "Enter PWD"

Import-PfxCertificate -FilePath $filelocale -CertStoreLocation Cert:\LocalMachine\My -Password $test.Password

标签: powershellwindows-10

解决方案


由于使用凭证对象可以解决问题,因此只需在不使用提示的情况下创建凭证:

$Pass = ConvertTo-SecureString -String 'plaintextpassword' -Force -AsPlainText
$User = "whatever"
$Cred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User, $Pass
Import-PfxCertificate -FilePath $filelocale -CertStoreLocation Cert:\LocalMachine\My -Password $Cred.Password

请注意我用来阻止 PowerShell 解释输入的单引号。这对于任何带有符号的密码尤其重要。如果在“plaintextpassword”周围使用双引号时使用凭证对象不起作用,则问题可能只是您输入的内容而不是您在最终结果中实际获得的内容。

如果您想自己检查,可以使用基于“在 Windows PowerShell 中使用密码、安全字符串和凭据”的以下内容。绝对值得一读。

$Pass = ConvertTo-SecureString -String "plaintext$_password" -Force -AsPlainText
$User = "whatever"
$Cred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User, $Pass
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Cred.Password)
$PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
$PlainPassword

请特别注意使用双引号和$_导致问题的其他内容。如果您使用的是最新的 PowerShell 或其他具有突出显示功能的工具,颜色格式可能会帮助您更早地发现问题。你可能会明白我的意思。


推荐阅读