I am attempting to utilise Powershell to automate the creation of some PFX certs using Openssl. I am trying to create a foreach loop that takes both the private pem key and the certificate that was issued and pushes out a pfx file using the openssl pkcs12 command. I have attempted to create an object on which the foreach loop can grab the data but I don't believe the correct value is being parsed through to the openssl command.
$openssldir = "C:\openssl-1.1\x64\bin"
$certlocation = "C:\openssl-1.1\x64\bin\Certs"
$keylocation = "C:\openssl-1.1\x64\bin\Keys"
$pfxlocation = "C:\openssl-1.1\x64\bin\PFX"
$certs = (Get-ChildItem -path $certlocation -File).Name
$keys = (Get-ChildItem -path $keylocation -File).Name
$certpath = $certs | foreach{".\Certs\" + $_}
$keypath = $keys | foreach{".\Keys\" + $_}
set-location -Path $openssldir
$a = @()
$obj = New-Object PSObject
$obj | Add-Member -type NoteProperty -Name 'cert' -Value $certpath
$obj | Add-Member -Type NoteProperty -Name 'key' -Value $keypath
$a += $obj
ForEach($item in $a){
$pfx = $item.cert
$pfxfile = $pfx.replace(".cer",".pfx")
.\openssl.exe pkcs12 -export -out PFX/$pfxfile -inkey $item.key -in $item.cert -password pass:
}
I can run the command manually and it works no problem and I can even run the command using the specific entries in the array like:
.\openssl.exe pkcs12 -export -out PFX/test.pfx -inkey $item.key[0] -in $item.cert[0] -password pass:
I've probably just got some syntax wrong or something stupid so any help is appreciated!
Ryan