Created
May 12, 2022 00:44
-
-
Save jadiagaurang/5ea5f70f27d62e8d16fa97baad05b91b to your computer and use it in GitHub Desktop.
Convert PFX files from a folder to PEM using OpenSSL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Clear Terminal | |
Clear-Host | |
# Declare iVars | |
$pfxPasswd = "scott/tiger" | |
$root_directory = "C:\SSLs" | |
$files = Get-ChildItem $root_directory -Filter *.pfx | |
# Change Directory to RootDirectory | |
Set-Location $root_directory | |
foreach ($f in $files) { | |
$file = $f.FullName | |
$fileName = (Get-Item $file).Name | |
$fileWithoutExtension = (Get-Item $file).Basename | |
$domainName = $fileWithoutExtension -replace "_","*" | |
Write-Host "" | |
Write-Host $("----- Starting Certificate " + $domainName + " -----") | |
# Get SSL Certificate | |
$pemPrivateFile = ($fileWithoutExtension + "-private.pem") | |
Invoke-Expression "openssl pkcs12 -in $fileName -nocerts -out $pemPrivateFile -nodes -password pass:$pfxPasswd" | |
# Get Client Certificate | |
$crtFile = ($fileWithoutExtension + ".crt") | |
Invoke-Expression "openssl pkcs12 -in $fileName -nokeys -out $crtFile -nodes -clcerts -password pass:$pfxPasswd" | |
# Get Intermediate Certificate CA Certificate | |
$cacrtFile = ($fileWithoutExtension + "-ca.crt") | |
Invoke-Expression "openssl pkcs12 -in $fileName -nokeys -out $cacrtFile -nodes -cacerts -password pass:$pfxPasswd" | |
$pemFile = ($fileWithoutExtension + ".pem") | |
Get-Content $pemPrivateFile, $crtFile, $cacrtFile | Set-Content $pemFile | |
Write-Host "Moving to new Directory..." | |
New-Item -Force -Path $root_directory -Name $fileWithoutExtension -ItemType "directory" | |
Move-Item -Force -Path $fileName -Destination $($root_directory + "\" + $fileWithoutExtension) | |
Move-Item -Force -Path $pemPrivateFile -Destination $($root_directory + "\" + $fileWithoutExtension) | |
Move-Item -Force -Path $crtFile -Destination $($root_directory + "\" + $fileWithoutExtension) | |
Move-Item -Force -Path $cacrtFile -Destination $($root_directory + "\" + $fileWithoutExtension) | |
Move-Item -Force -Path $pemFile -Destination $($root_directory + "\" + $fileWithoutExtension) | |
Write-Host $("----- Completed Certificate " + $domainName + " -----") | |
Write-Host "" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment