Automated Certificate Deployment with Powershell

Let’s say I need to deploy different certificates to different certificate stores. For this exercise I am going to use locations.txt, certificates.txt and password.txt files.

The structure of the files look as per below.

Locations.txt

cert:\localmachine\my
cert:\localmachine\root
cert:\localmachine\ca

Certificates.txt

c:\certificate\cert\ServiceFabricClusterPrimaryCert.pfx
c:\certificate\cert\ServiceFabricSecCert.pfx
c:\certificate\cert\ServiceFabricNonAdminCert.pfx
c:\certificate\cert\ServiceFabricAdminCert.pfx

Password.txt

SBG7EmKM
BcwBkV4w
ATknFuF9
RThc7YXj

And the powershell script.

$Location = Get-Content -Path "c:\certificate\Locations.txt"
$CertPath = Get-Content -Path "c:\certificate\Certificates.txt"
$Pass = Get-Content -Path "c:\certificate\password.txt"

For ($i=0; $i -lt $Location.LongLength; $i++)
{
    Import-PfxCertificate -Exportable -CertStoreLocation $Location[$i] -FilePath $CertPath[$i] -Password (ConvertTo-SecureString -String $Pass[$i] -AsPlainText -Force)

}

Leave a Reply

Your email address will not be published.