【问题标题】:Automatically sign powershell script using Get-PfxCertificate使用 Get-PfxCertificate 自动签署 powershell 脚本
【发布时间】:2011-01-20 03:46:49
【问题描述】:

我必须使用来自远程机器的证书签署远程脚本,我从中拥有一个.pfx 文件。

我想通过以编程方式向Get-PfxCertificate 提供密码来自动执行脚本。

所以问题是:

是否有可能以某种方式以编程方式向

提供所需的密码

Get-PfxCertificate?

【问题讨论】:

    标签: powershell certificate


    【解决方案1】:

    我对此进行了一些检查,但找不到以编程方式提供密码的干净方法。我怀疑出于安全原因,它应该是这种方式。要么是那个,要么是 PowerShell 开发团队通过不包含此 cmdlet 的凭据参数而搞砸了。我能想到的唯一其他选择是使用像 SendKeys 这样的东西通过后台作业在正确的时间将单个密码字符按键发送到 PowerShell 控制台(blech - 只是在我嘴里吐了一点)。 :-)

    【讨论】:

    【解决方案2】:
    $CertPath = "my.pfx"
    $CertPass = "mypw"
    $Cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($CertPath, $CertPass)
    Set-AuthenticodeSignature -Certificate $Cert -TimeStampServer http://timestamp.verisign.com/scripts/timstamp.dll -FilePath $OutputFilename
    

    确保您拥有适当的权限,否则您将无法创建 X509Certificate2 对象的实例。

    【讨论】:

    • 很好的答案。您也可以为此使用 Get-PfxData。您需要提供密码作为 SecureString 和 EndEntityCertificates 以使用证书而不被提示。使用您的示例:` $SecurePassword=ConvertTo-SecureString -String "mypw" -AsPlainText -Force; $PfxData=Get-PfxData -FilePath "my.pfx" -Password $SecurePassword; Set-AuthenticodeSignature -Certificate $PfxData.EndEntityCertificates[0] -TimeStampServer timestamp.verisign.com/scripts/timstamp.dll -FilePath $OutputFilename; `
    【解决方案3】:

    执行此操作的另一种方法是使用 PS 提供程序直接从您的证书存储加载您的证书。使用 Get-PSProviders 确定您机器上可用的 PSProviders。 加载证书提供程序后,您现在可以使用 Get-ChildItem 获取证书

    从运行中启动 certmgr.msc 以启动证书存储
    假设您的证书存储在证书存储
    Personal 文件夹下,并且在证书的主题属性中设置了“Company Name”,并且只有证书在主题中带有 Company Name 的文件夹中 - 您可以像这样获得证书

    $my_cert = Get-ChildItem cert:\CurrentUser\My | ? {$_.Subject -match "Company Name"}
    

    $my_cert 将是您可以直接传递给 Set-AuthenticodeSignature cmdlet

    的证书对象
    Set-AuthenticodeSignature -Certificate $my_cert -FilePath fqn_to_dll.dll -Timestampserver "http://timestampurl"
    

    签名后,您可以通过在 Status 属性上查询“有效”或不喜欢来检索签名状态

    $result = Set-AuthenticodeSignature -Certificate $my_cert -FilePath fqn_to_dll.dll -Timestampserver "http://timestampurl" | Select Status
    if(-Not ($result -eq "Valid")){
        Write-Output "Error Signing file: Status: $($result.Status)"    
    }
    

    【讨论】:

    • 不错的选择,但是如何避免密码提示?
    猜你喜欢
    • 2011-04-06
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 2022-11-11
    • 2012-04-18
    • 1970-01-01
    相关资源
    最近更新 更多