Powershell 导入带有密码的Pfx证书-部署

Powershell 导入带有密码的Pfx证书-部署,powershell,windows-10,Powershell,Windows 10,我想推出带有私钥密码的证书。我知道这不安全。一个更安全的方法会更好。但我想不出怎么做 $test = ConvertTo-SecureString -String "plaintextpassword" -Force -AsPlainText<code> Import-PfxCertificate -FilePath $filelocale -CertStoreLocation Cert:\LocalMachine\My -Password $test Getting:Impor

我想推出带有私钥密码的证书。我知道这不安全。一个更安全的方法会更好。但我想不出怎么做

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

Import-PfxCertificate -FilePath $filelocale -CertStoreLocation Cert:\LocalMachine\My -Password $test
Getting: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

由于使用凭证对象就可以做到这一点,因此只需创建凭证而不使用提示:

$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”周围使用双引号时使用凭证对象不起作用,那么问题很可能只是您输入的内容不是最终结果中实际得到的内容

如果你想自己检查它,你可以根据“.”使用以下内容。这绝对值得一读

$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

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

您如何运行第一个命令?i、 剧本?在同一个PowerShell控制台中运行两组命令是否有效?如果将其转换为凭据,是否有效<代码>$cred=新对象系统、管理、自动化、PSCredential(“输入pwd”、$test)