Converting Numeric Property value to alternate text

I am creating a script to obtain metric information from our Enterprise CA. Specifically in this case - The Previous Month’s revoked certificates.

I have been able to obtain the information I am looking for but one property of a revoked certificate is the reason code. These codes are numeric and correspond to the various reasons of why a certificate is revoked. When I run my query of the DB I can get back the numeric value of the reason code but I want to transform that numeric value into human readable.

My code is after connecting to the certDb and specifying the restrictions for the query is…

$Row = $CaView.OpenView()
$cert = New-Object psobject -Property @{Configstring = $ConfigString;}

$NumExpcerts = 0
While ($row.Next() -ne -1) {
$Column_Enum = $row.EnumCertViewColumn()
while ($Column_Enum.Next() -ne -1)
{
$current = $Column_Enum.GetName()

   $cert | Add-Member -MemberType NoteProperty $($Column_Enum.GetName()) -value $($Column_Enum.GetValue(1)) -force     

}

$cert | Select-Object -Property @{name=“Status”;expression={if ($cert.‘request.disposition’ -eq “21”) {write “Revoked”}}},…

At the end I want to return the property for revoked.reason and change the Numeric value to Human readable. I just cant figure out how to do it.

The mapping is below.

0 = “Unspecified”
1 = “KeyCompromise”
2 = “CACompromise”
3 = “AffiliationChange”
4 = “Superceded”
5 = "CeaseofOperation’
6 = “Hold”
8 = “ReleaseFormCRL”

You can use a switch statement, like so

switch ($exitCode){
    0 {$reason = 'Unspecified'}
    1 {$reason = 'KeyCompromise'}
    2 {$reason = 'CACompromise'}
    3 {$reason = 'AffiliationChange'}
    4 {$reason = 'Superceded'}
    5 {$reason = 'CeaseofOperation'}
    6 {$reason = 'Hold'}
    7 {$reason = 'ReleaseFormCRL'}
DEFAULT {$reason = 'Unspecified'}
}

Then you have the correct value in the $reason variable.

To add to Oyvind’s answer, I like to use hashtables for this:

$ExitCodes = @{
    0 = 'Unspecified'
    1 = 'KeyCompromise'
    2 = 'CACompromise'
    3 = 'AffiliationChange'
    4 = 'Superceded'
    5 = 'CeaseofOperation'
    6 = 'Hold'
    7 = 'ReleaseFormCRL'
}

# Do some stuff that outputs a code
$ProcessExit = 3

$ExitCodes.$ProcessExit

Hey Matt. I have actually not thought about using hash tables for this. Cool idea :slight_smile: Thanks!

I was able to get the switch to work. It was a matter of where I was placing it in my script.

Thanks.