Get-CertificateTemplate.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<#
    .SYNOPSIS
        This commandlet output the certificate template name
 
    .DESCRIPTION
        The certificate template name is encapsulated in the Extentions. The commandlets gives a human readable value as you see also from the UI.
 
    .PARAMETER Certificate
        A X509Certificate2 instance
 
    .EXAMPLE
        Get-ChildItem "Cert:\LocalMachine\My" | Get-CertificateTemplate
 
    .EXAMPLE
        Get-ChildItem "Cert:\LocalMachine\My" | Select-Object Name,Thumbprint,@{Name="Template";Expression={Get-CertificateTemplate $_}}
 
    .INPUTS
        Any X509Certificate2 certificate
 
    .OUTPUTS
        The certificate template name
 
    .LINK
        https://social.technet.microsoft.com/Forums/ie/en-US/187698d0-5602-4301-9d0c-85e89d948ea2/user-powershell-to-get-the-template-used-to-create-a-certificate
#>

function Get-CertificateTemplate {
    [OutputType([string])]
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [Security.Cryptography.X509Certificates.X509Certificate2]$Certificate
 )
    Process {
        $temp = $Certificate.Extensions | ?{$_.Oid.Value -eq "1.3.6.1.4.1.311.20.2"}
        if (!$temp) {
            $temp = $Certificate.Extensions | ?{$_.Oid.Value -eq "1.3.6.1.4.1.311.21.7"}
        }
        #Sometimes $temp is null
        if($temp){
            $temp.Format(0)
        }
        else
        {
            Write-Warning "Cannot evaluate certificate template"
            "Unknown"
        }
    }
}