Retired Microsoft Blog disclaimer

This directory is a mirror of retired "Decrypt My World" MSDN blog and is provided as is. All posting authorship and copyrights belong to respective authors.
Original URL: https://blogs.msdn.microsoft.com/alejacma/2012/04/13/how-to-export-issued-certificates-from-a-ca-programatically-powershell/
Post name: How to export issued certificates from a CA programatically (PowerShell)
Original author: Alejandro Campos Magencio
Posting date: 2012-04-13T05:56:38+00:00


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)


Share this article:

Comments:

Comments are closed.