Created
September 13, 2021 21:43
-
-
Save HarmJ0y/eb2d41b68cd62c8895a80ca4219bc0a6 to your computer and use it in GitHub Desktop.
Revisions
-
HarmJ0y created this gist
Sep 13, 2021 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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