PowerShell - Gather information from your Certificate Server

2011/01/28 | 1 minute read |

Here is a quick PowerShell tip to retrieve information from your Certificate Authority server

PowerShell version

Function Get-CertInfo {

    # Establish connection to Certificate server
    $CaView = New-Object -Com CertificateAuthority.View.1

    # Define the numbers of columns
    $Index0 = $CAView.GetColumnIndex($False, "CommonName")
    $Index1 = $CAView.GetColumnIndex($False, "Email")
    $Index2 = $CAView.GetColumnIndex($False, "NotAfter")
    $Index3 = $CAView.GetColumnIndex($False, "Country")
    $Index4 = $CAView.GetColumnIndex($False, "Organization")
    $Index5 = $CAView.GetColumnIndex($False, "OrgUnit")
    $Index6 = $CAView.GetColumnIndex($False, "DistinguishedName")
    $Index7 = $CAView.GetColumnIndex($False, "Disposition")
    $RowObj= $CAView.OpenView()
        $Cert= $Cert + $srv + ","
        $ColObj = $RowObj.EnumCertViewColumn()
        Do {
            $Cert = $Cert + $ColObj.GetValue(1) + ","
        } Until ($ColObj.Next() -eq -1)
        Clear-Variable ColObj
    } Until ($Rowobj.Next() -eq -1 )
    Return $Cert

VbScript version

Const CV_OUT_BASE64 = &H1

'THIS IS THE <machinename>\CAName
CAName = "MyMachine\SpatCA"     '=======>> CHANGE THIS TO THE CORRECT MACHINE\CA==

'create the CAView object
set oCAView = CreateObject("CertificateAuthority.View.1")

'open the connection to the Machine\CA
oCAView.OpenConnection (CAName)

'retrieve specific columns from DB
Index0 = oCAView.GetColumnIndex(False, "CommonName") 
Index1 = oCAView.GetColumnIndex(False, "Email")
Index2 = oCAView.GetColumnIndex(False, "NotAfter")

oCAView.SetResultColumn (Index0) 
oCAView.SetResultColumn (Index1)
oCAView.SetResultColumn (Index2)

'open the view
Set RowObj= oCAView.OpenView

Do Until RowObj.Next = -1

Set ColObj = RowObj.EnumCertViewColumn()

Do Until ColObj.Next = -1
wscript.echo  ColObj.GetValue(CV_OUT_BASE64) & vbcrlf
'insert logic for checking date to
'current and if near\past send mail.
'see http://www.paulsadowski.com/WSH/cdo.htm
'for a number of examples of mail send info
'Obviously you may want to use the cert email
'attribute to send the mail

Set ColObj = Nothing