Created
February 12, 2018 15:28
-
-
Save jabez007/ca6b7ac9e925fed50ed95ef81f885747 to your computer and use it in GitHub Desktop.
A PowerShell script to mass export client certificates from a server
This file contains hidden or 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
param( | |
[Parameter(Mandatory = $true)] | |
[String[]]$Thumbprints, | |
[Parameter(Mandatory = $true)] | |
[String]$Password | |
) | |
#### Usage #### | |
# .\Export-ClientCertificates.ps1 -Thumbprints "array","of","thumbprints","and","subject","common","names" -Password "PrivateKeyPassword" | |
#### | |
$passwd = ConvertTo-SecureString -String $Password -Force -AsPlainText | |
$certPath = "Cert:\LocalMachine\My" | |
ForEach ($certprint in $Thumbprints) { | |
$cert = Get-ChildItem $certPath -Recurse | ? { $_ -is [System.Security.Cryptography.X509Certificates.X509Certificate2] } | ? {$_.Thumbprint -eq $certprint.Replace(" ","").ToUpper()} | |
if ($cert -eq $null) { | |
$cert = Get-ChildItem $certPath -Recurse | ? { $_ -is [System.Security.Cryptography.X509Certificates.X509Certificate2] } | ? {$_.SubjectName.Name.ToUpper() -like "*CN=$certprint,*".ToUpper()} | |
} | |
if (-Not ($cert -eq $null)) { | |
$certSubjectName = $cert.SubjectName.Name | |
#Write-Host "Found Subject Name: $certSubjectName" | |
if ($cert.SubjectName.Name -match "CN=(?<commonName>[^,]*)") { | |
$certCommonName = $Matches['commonName'] | |
#Write-Host "Found Common Name: $certCommonName" | |
$certThumbprint = $cert.Thumbprint | |
#Write-Host "Found Thumbprint: $certThumbprint" | |
$exportFile = "$certCommonName ($certThumbprint).pfx" | |
#Write-Host "Writing to $exportFile" | |
$cert | Export-PfxCertificate -FilePath $exportFile -Password $passwd -ChainOption BuildChain | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment