Powershell:使用循环将证书转换为 pfx

Powershell: Convert certificates to pfx with a loop

我正在尝试使用循环将一堆证书转换为 pfx 格式,因为有很多证书。我 运行 遇到的问题是关于私钥的。我不确定如何使用正确的证书循环正确的私钥。下面是我的循环

Get-ChildItem -Path $CertPath\SCP -Recurse -Include "*cer", "*.key" | 
ForEach-Object {
    $OutFile = $_.FullName.ToString().Replace(".cer",".pfx")
    $Password = Read-host "enter the password" -AsSecureString
    openssl pkcs12 -export -in $CertPath\Folder -inkey $PrivateKey -out $Outfile -password $Password}

如果密钥和证书文件的 Basename 匹配,只需再做一次替换:

$key = $_.FullName.ToString().Replace('.cer','.key')