How to export issued certificates from a CA programatically (PowerShell)
Hi all,
The following sample is a conversion of How to export issued certificates from a CA programatically (C#) sample to PowerShell. It will get all the issued certs in the CA database and copy them to a folder:
#Params
$strServer = "myserver";
$strCAName = "myserver-CA";
$strPathForCerts = "c:\test\";
# Constants
$CV_OUT_BASE64HEADER = 0;
$CV_OUT_BINARY = 2;
# Connecting to the Certificate Authority
$objCertView = New-Object -ComObject CertificateAuthority.View
$objCertView.OpenConnection($strServer + "\" + $strCAName)
# Get a column count and place columns into the view
$iColumnCount = $objCertView.GetColumnCount(0)
$objCertView.SetResultColumnCount($iColumnCount)
# Place each column in the view
for ($x=0; $x -lt $iColumnCount; $x++)
{
$objCertView.SetResultColumn($x)
}
# Open the View and reset the row position
$objCertViewRow = $objCertView.OpenView();
$objCertViewRow.Reset();
# Enumerate Row and Column Information
# Rows (one per cert)
for ($x = 0; $objCertViewRow.Next() -ne -1; $x++)
{
# Columns with the info we need
$objCertViewColumn = $objCertViewRow.EnumCertViewColumn()
while ($objCertViewColumn.Next() -ne -1)
{
switch ($objCertViewColumn.GetDisplayName())
{
"Request ID"
{
#Request ID
$objValue = $objCertViewColumn.GetValue($CV_OUT_BINARY)
if ($objValue -ne $null) {
$strID = "Request ID " + $objValue
}
break
}
"Binary Certificate"
{
# Binary Certificate
$objValue = $objCertViewColumn.GetValue($CV_OUT_BASE64HEADER);
if ($objValue -ne $null) {
# Write certificate to file
$strPath = $strPathForCerts + $strID + ".cer"
Set-Content $strPath $objValue
}
break
}
default
{
}
}
}
}
Write-Host "We are done!`nCerts have been copied to " + $strPathForCerts
I hope it helps.
Regards,
Alex (Alejandro Campos Magencio)