Skip to content

Instantly share code, notes, and snippets.

@HarmJ0y
Created September 13, 2021 21:43
Show Gist options
  • Save HarmJ0y/eb2d41b68cd62c8895a80ca4219bc0a6 to your computer and use it in GitHub Desktop.
Save HarmJ0y/eb2d41b68cd62c8895a80ca4219bc0a6 to your computer and use it in GitHub Desktop.

Revisions

  1. HarmJ0y created this gist Sep 13, 2021.
    26 changes: 26 additions & 0 deletions dc_cert_template.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    $Results = ([adsisearcher]"(&(objectCategory=computer)(userAccountControl:1.2.840.113556.1.4.803:=8192))").FindAll() | % {
    $Entry = $_.GetDirectoryEntry()

    $SAM = $Entry.samAccountName[0]
    $DN = $Entry.distinguishedName[0]

    try {
    $Cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 @($Entry.userCertificate)
    $Exp = $Cert.GetExpirationDateString()

    $TemplateExt = $Cert.Extensions | Where-Object{ ( $_.Oid.FriendlyName -eq 'Certificate Template Name') } | Select-Object -First 1

    [pscustomobject] @{
    'SamAccountName'=$SAM
    'DistinguishedName'=$DN
    'Thumprint'=$Cert.Thumbprint
    'Expiration'=$Cert.GetExpirationDateString()
    'TemplateName'=$TemplateExt.Format(1)
    }
    }
    catch {
    Write-Warning "Error retrieving the certificate from DC '$DN' !"
    }
    }

    $Results | fl