7

次のようなコマンドを使用します。

get-pfxcertificate C:\test.pfx

パスワードを入力する: *******

コマンドは、プロンプトに入力するように求めます。しかし、スクリプトでそれを行うことはできません (ex の場合は test.ps1)。

私が必要とするのは次のようなものです:

get-pfxcertificate C:\test.pfx -パスワード "123456"

または似たようなもので、毎回プロンプトを入力せずにスクリプトを実行できます

私はどんな返事にもとても感謝しています

4

5 に答える 5

20

Password パラメーターはありません。.NET クラスで試すことができます。

$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$cert.Import('C:\test.pfx','123456','DefaultKeySet')
于 2012-12-27T09:42:26.577 に答える
6

もう 1 つのオプションは、 の機能を拡張して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
于 2015-06-02T17:41:19.070 に答える
2

私を正しい方向に向けてくれたシェイに感謝します。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
于 2014-04-01T14:23:39.387 に答える
-2

これは、.NET の代わりにネイティブ PowerShell を使用しても機能します。

$securePassword = ConvertTo-SecureString -String $strPW -Force -AsPlainText
$cert = Import-PfxCertificate -FilePath $strPFX cert:\LocalMachine\My -Password $securePassword
于 2014-09-08T16:27:11.233 に答える