powershell - 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
解决方案
由于使用凭证对象可以解决问题,因此只需在不使用提示的情况下创建凭证:
$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 或其他具有突出显示功能的工具,颜色格式可能会帮助您更早地发现问题。你可能会明白我的意思。
推荐阅读
- node.js - 在 guild.emojis.create() 之后发送一个新创建的表情符号
- c - 从气体组件链接和调用 printf
- ssl - 带有 SSL 加密的 Mosquitto 代理,用于桥接连接
- java - 错误或功能?解析本地化月份名称的 Java 16 DateTimeFormatter 问题
- .net - XmlSerializer 反序列化简单对象列表
- amazon-cloudwatch - 如何在这么多数据点后将 AWS CloudWatch 警报设置为“OK”状态?
- python - 使用 MultiIndex 提供给 DataFrame 的数据结构
- java - 如何将当前时间与我的 TimePickerDialog 时间进行比较?
- java - 即使在设置宽度以匹配父级之后,android底部应用栏也没有采用全宽
- sql-server - WAITFOR 延迟/时间在 Windows Docker (SQL Server Linux) 中无法正常工作