Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically sign powershell script using Get-PfxCertificate

I have to sign remote scripts with a certificate from the remote machine from which I have a .pfx file.

I would like to automate the scripting by supplying the password to the Get-PfxCertificate programmatically.

So the question is:

Is it possible to somehow supply programmatically the required password to

Get-PfxCertificate?

like image 515
El Toro Bauldo Avatar asked Dec 23 '22 05:12

El Toro Bauldo


2 Answers

$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

Make sure you have the proper permissions otherwise you won't be able to create an instance of the X509Certificate2 object.

like image 51
Nathan Moinvaziri Avatar answered Jan 18 '23 23:01

Nathan Moinvaziri


I did a bit of checking around on this and couldn't find a clean way to provide the password programmatically. I suspect it is meant to be this way for security reasons. Either that or the PowerShell development team just blew it by not including a Credential parameter for this cmdlet. The only other option I can think of is to use someting like SendKeys to send the individual password character key presses to the PowerShell console at the right time via a background job (blech - just threw up in my mouth a little). :-)

like image 39
Keith Hill Avatar answered Jan 18 '23 23:01

Keith Hill