【问题标题】:How to fill the prompt in powershell script如何在powershell脚本中填写提示
【发布时间】:2012-12-27 03:25:52
【问题描述】:

我使用这样的命令:

获取 pfx 证书 C:\test.pfx 输入密码: *******

该命令要求我填写提示。但我不能在我的脚本中这样做(test.ps1 for ex)

我需要的是这样的:

获取-pfxcertificate C:\test.pfx -password "123456"

或类似的东西,这样我就可以运行我的脚本而无需每次都填写提示

非常感谢您的回复

【问题讨论】:

    标签: powershell


    【解决方案1】:

    没有密码参数,你可以试试.NET类:

    $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
    $cert.Import('C:\test.pfx','123456','DefaultKeySet')
    

    【讨论】:

    • [void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms") [System.Windows.Forms.SendKeys]::SendWait("123456~")
    • 这也可以解决问题,但对我来说还不够好,因为如果 get-pfxcertificate 不要求提示,上述命令将暴露我的密码。你的解决方案真的很好。非常感谢
    • @ngubk,您可以使用Read-Host 提示输入密码。 stackoverflow.com/a/30602978/124069
    【解决方案2】:

    另一种选择是扩展Get-PfxCertificate 的功能,实质上是允许输入密码。

    # create a backup of the original cmdlet
    if(Test-Path Function:\Get-PfxCertificate){
        Copy Function:\Get-PfxCertificate Function:\Get-PfxCertificateOriginal
    }
    
    # create a new cmdlet with the same name (overwrites the original)
    function Get-PfxCertificate {
        [CmdletBinding(DefaultParameterSetName='ByPath')]
        param(
            [Parameter(Position=0, Mandatory=$true, ParameterSetName='ByPath')] [string[]] $filePath,
            [Parameter(Mandatory=$true, ParameterSetName='ByLiteralPath')] [string[]] $literalPath,
    
            [Parameter(Position=1, ParameterSetName='ByPath')] 
            [Parameter(Position=1, ParameterSetName='ByLiteralPath')] [string] $password,
    
            [Parameter(Position=2, ParameterSetName='ByPath')]
            [Parameter(Position=2, ParameterSetName='ByLiteralPath')] [string] 
            [ValidateSet('DefaultKeySet','Exportable','MachineKeySet','PersistKeySet','UserKeySet','UserProtected')] $x509KeyStorageFlag = 'DefaultKeySet'
        )
    
        if($PsCmdlet.ParameterSetName -eq 'ByPath'){
            $literalPath = Resolve-Path $filePath 
        }
    
        if(!$password){
            # if the password parameter isn't present, just use the original cmdlet
            $cert = Get-PfxCertificateOriginal -literalPath $literalPath
        } else {
            # otherwise use the .NET implementation
            $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
            $cert.Import($literalPath, $password, $X509KeyStorageFlag)
        }
    
        return $cert
    }
    

    现在你可以调用它了

    # tada: extended cmdlet with `password` parameter
    Get-PfxCertificate 'C:\path\to\cert.pfx' 'password'
    

    另外,如果你仍然需要提示,你可以这样做。

    $pwd = Read-Host 'Please enter your SSL Certificate password.'
    Get-PfxCertificate 'C:\path\to\cert.pfx' $pwd
    

    【讨论】:

      【解决方案3】:

      现在 PowerShell 中有一个 Get-PfxData 命令可以获取证书和链。该命令包含一个 -Password 参数,该参数采用 SecureString 对象,因此您可以避免被提示。

      EndEntityCertificates 属性包含证书链末尾的证书数组,并将包含由Get-PfxCertificate 命令创建的相同证书对象。

      以下示例将普通字符串转换为 SecureString 对象,从文件加载证书,然后将第一个/唯一结束证书分配给 $SigningCert 变量:

      $SecurePassword=ConvertTo-SecureString -String "MyPassword" -AsPlainText -Force
      $PfxData=Get-PfxData -FilePath ".\cert_filename.pfx" -Password $SecurePassword
      $SigningCert=$PfxData.EndEntityCertificates[0]
      

      您现在可以在不提示输入密码的情况下应用 $SigningCert。

      【讨论】:

        【解决方案4】:

        感谢 Shay 为我指明了正确的方向。 我需要从 PFX 文件中获取指纹,所以我使用了非持久性 DefaultKeySet。 除非密钥集完全合格,否则 2012 PS3 下的测试将失败。 此外,Import 和左括号之间的空格,即“$cert.Import^^(Sys...”会导致错误。挑剔,挑剔的解析器。

        我的 PFX 密码在源代码中已加密。 我在运行时解密它,所以它在源中不可见。

        Set-StrictMode -Version Latest
        [string] $strPW  = '123456'
        [string] $strPFX = 'C:\MyCert.pfx'
        
        $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
        $cert.Import($strPFX,$strPW,[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]"DefaultKeySet")
        $cert.Thumbprint
        

        【讨论】:

          【解决方案5】:

          这也适用于使用本机 PowerShell 而不是 .NET:

          $securePassword = ConvertTo-SecureString -String $strPW -Force -AsPlainText $cert = Import-PfxCertificate -FilePath $strPFX cert:\LocalMachine\My -Password $securePassword

          【讨论】:

          • OP 没有使用Import,他们使用的是get,默认情况下没有密码参数。
          • 确实,这个答案令人困惑。 PowerShell 3.0 的本机 Get-PfxCertificate Cmdlet 在 .pfx 文件上提供了很多功能,但不允许将密码作为 SecureString 传递。这个答案是关于 PKI 模块的 Import-PfxCertificate CmdLet,它确实接受密码参数,但相比之下缺乏功能。
          猜你喜欢
          • 1970-01-01
          • 2020-11-20
          • 2021-06-30
          • 1970-01-01
          • 2018-11-21
          • 2021-09-01
          • 2015-02-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多