How to use Powershell to import Server certificates


The following script is used to import a .pfx certificate to the local machine store on a Windows Server:

$servers = Get-Content “servers.txt”
$cert = Get-ChildItem | Where-Object { $_.Extension -eq “.pfx” }
$certpass = “sp8lp/Mih!tbsyc”

$WorkingCert = Get-ChildItem CERT:\LocalMachine\My | where {$_.Subject -match $cert} | select -first 1
$TPrint = $WorkingCert.Thumbprint
$certpath = “Cert:\localmachine\My\” + $TPrint

foreach ($s in $servers)
{

if (Test-Path -Path $certpath )
{
write-host “Certificate $cert already exists on $s”
Break
}
else
{
Import-PfxCertificate -FilePath $cert -certstorelocation Cert:\localmachine\My -Password (ConvertTo-SecureString -String $certpass -AsPlainText -Force)
Start-Sleep -Seconds 2

if (!(Test-Path -Path $certpath))
{
write-host “Certificate $cert imported successfully on $s”
}
else
{
Write-Host “Certificate $cert not imported successfully on $s”
}
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s