Finding the template of a certificate

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Dan Potter Dan Potter 10 months ago.

  • Author
    Posts
  • #34997
    Profile photo of Justin King
    Justin King
    Participant

    I'm trying to find a nice "clean" way of displaying the template name used in the creation of a certificate in PowerShell. That information is currently in an OID, which makes the output a bit frustrating for me. Quick example below:

    $Certificate = get-childitem cert:\LocalMachine\My\THUMPRINTHERE
    $Template = ($Certificate.extensions | where-object{$_.oid.FriendlyName -match "Certificate Template Information"}).format(0).split(",")[0]
    

    So this gives me an "almost there" output string of this: "Template=Lab Web Server(long OID in here)". I'd like the output of the string to be simply "Lab Web Server". no a better way to clean this data up?

    EDIT: I found a way to do it that feels a little messy:

    $template.trimstart("Template=").split("(")[0]
    

    Not sure if there's a cleaner way to handle this....

  • #35001
    Profile photo of Dan Potter
    Dan Potter
    Participant

    Google 'The string's the thing'

    foreach($cert in $certs){

    [pscustomobject]@{
    Template = $cert.extensions.Format(1)[0].split('(')[0] -replace "template="
    Subject = $cert.subject
    }

    }

  • #35003
    Profile photo of Dan Potter
    Dan Potter
    Participant

    Your way would work too. When you're dealing with output like this it's not pretty.

    ($cert.extensions.Format(0) -replace "(.+)?=(.+)\((.+)?", '$2')[0]

You must be logged in to reply to this topic.