【问题标题】:Powershell generated pfx file password problemPowershell生成pfx文件密码问题
【发布时间】:2020-03-09 01:04:35
【问题描述】:

我已关注 msdn article 使用 powershell 生成 pfx 文件。所以我已经按顺序执行了

New-SelfSignedCertificate -Type Custom -Subject "CN=Contoso Software, O=Contoso Corporation, C=US" -KeyUsage DigitalSignature -FriendlyName "Your friendly name goes here" -CertStoreLocation "Cert:\CurrentUser\My" -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.3", "2.5.29.19={text}")

$pwd = ConvertTo-SecureString -String mypassword -Force -AsPlainText 
Export-PfxCertificate -cert "Cert:\CurrentUser\My\<Certificate Thumbprint>" -FilePath mycert.pfx -Password $pwd

到目前为止,一切都很好,但是当我尝试使用下面的命令获取 pfx 证书时。它提示输入我使用的密码。我正在尝试与“mypassword”完全相同的密码,但它在下面返回错误

 Get-PfxCertificate -FilePath mycert.pfx

Get-PfxCertificate : 指定的网络密码不正确。在 行:1 字符:1

我正在尝试使用此证书和密码在 azure devops 上创建构建管道,但 azure pipeliness 给了我相同的错误消息。

如果我使用 Visual Studio 2019 创建 pfx 文件,使用我提供的密码获取 PfxCertificate 可以正常工作。但 Azure 管道返回另一个错误。

所以我的问题是,

  1. 使用 Visual Studio 2019 与 PowerShell 创建 pfx 文件有什么区别?为什么 VS 2019 生成的 pfx 不起作用。

  2. 为什么 Get-PfxCertificate 不接受我用来生成的密码?

关于第二个问题,我认为这可能与纯文本与 SecureString 有关。这就是为什么我尝试以下命令以及docs 中所述的原因。它告诉我 -password 无法识别。

   $pwd = ConvertTo-SecureString -String mypassword -Force -AsPlainText 
   Get-PfxCertificate -FilePath mycert.pfx -Password $pwd -NoPromptForPassword

错误:

Get-PfxCertificate : A parameter cannot be found that matches parameter name 'Password'.
At line:2 char:60

【问题讨论】:

    标签: uwp pfx


    【解决方案1】:
    1. 据我所知,应该没有太大区别,但老实说,我不太确定它在 VS 中是如何完成的

    2. Get-PFXCertificate cmdlet 的“-Password”参数仅在 PS 版本 6.1+ 中添加,可能检查您在 Azure 中使用的版本,因为我在本地遇到了同样的问题,这是因为我使用的是 5.1

    【讨论】:

      【解决方案2】:

      我不知道 1,但是对于 2,你不应该在 -Password 命令中使用大写的 P 吗?请参阅Powershell page: Get-Pfx​Certificate 以供参考,其中说明了以下内容;

      Get-PfxCertificate
         -LiteralPath <String[]>
         [-Password <SecureString>]
         [-NoPromptForPassword]
         [<CommonParameters>]
      

      如果问题仍然存在,显然还有其他事情发生,但我会先尝试一下,看看它是否可能因此无法识别命令。

      【讨论】:

      • 不,同样的错误。我认为它不区分大小写。
      • 是同样的错误还是同样的错误? Powershell 有时会给出“错误 - 不起作用”,而不会详细说明出了什么问题,在这种情况下,这可能是其他问题。考虑在代码中添加 try/catch 吗?
      • 完全相同的错误。你可以很容易地测试它。我可能需要 2 分钟来测试 :)
      【解决方案3】:

      我只是想为其他遇到我这个愚蠢问题的人提供一个答案。如果您在 powershell 中使用 $ 之类的符号而不使用撇号或双引号,则生成仍然有效并且您获得 pfx 文件但密码与您设置的不同:) 这就是 azure devops 返回此错误的原因

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-08
        • 2021-08-19
        • 1970-01-01
        • 1970-01-01
        • 2012-11-30
        • 2022-08-16
        相关资源
        最近更新 更多