Private/Get-ADCSEnterpriseCA.ps1
|
<#
.SYNOPSIS Inventories Enterprise CAs registered in Active Directory. .DESCRIPTION Reads enterprise CA objects and correlates their certificates, server availability, and published certificate templates. .PARAMETER InventoryOnly Displays the inventory without updating the AD CS setup state. .COMPONENT Category: Sub Function .FUNCTIONALITY Builds CA certificate relationships and reports server availability and template status. #> function Get-ADCSEnterpriseCA { ################################################################################ ##### ##### ##### Get all Enterprise CAs from AD ##### ##### ##### ################################################################################ Param( [Parameter(ValueFromPipeline, Mandatory)] [string]$Server, [switch]$InventoryOnly ) $Info = Measure-FunctionRuntime -Begin # Get all Enterprise CAs from AD $ConfigNC = (Get-ADRootDSE -Server $Server).ConfigurationNamingContext $cas = Get-ADObject -LDAPFilter "(objectClass=pKIEnrollmentService)" ` -SearchBase "CN=Public Key Services,CN=Services,$ConfigNC" ` -Properties cn, dNSHostName, cACertificate, certificateTemplates, flags ` -Server $Server # Helper: build X509 objects from cACertificate (handles collections) <# .SYNOPSIS Converts CA certificate data from Active Directory into X.509 objects. .FUNCTIONALITY Parses each CA certificate byte array and returns valid X.509 certificates. #> function Get-CaCertificates { param([Microsoft.ActiveDirectory.Management.ADObject]$CaObject) $certs = @() foreach ($entry in $CaObject.cACertificate) { if ($entry -is [byte[]]) { try { $certs += [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($entry) } catch {} } } $certs } # Build CA inventory with cert info $inventory = foreach ($ca in $cas) { $certs = Get-CaCertificates -CaObject $ca foreach ($cert in $certs) { [PSCustomObject]@{ CAName = $ca.cn Server = $ca.dNSHostName Subject = $cert.Subject Issuer = $cert.Issuer Thumbprint = $cert.Thumbprint NotBefore = $cert.NotBefore NotAfter = $cert.NotAfter IsSelfSigned = ($cert.Subject -eq $cert.Issuer) HasTemplates = ($ca.certificateTemplates -ne $null -and $ca.certificateTemplates.Count -gt 0) AD_Flags = $ca.flags } } } # Build hierarchy: link issuing CAs to their parent by Subject/Issuer $roots = $inventory | Where-Object { $_.IsSelfSigned } $issuing = $inventory | Where-Object { -not $_.IsSelfSigned } $hierarchy = foreach ($ca in $inventory) { $parent = $null if (-not $ca.IsSelfSigned) { $parent = $roots + $issuing | Where-Object { $_.Subject -eq $ca.Issuer } | Select-Object -First 1 } [PSCustomObject]@{ CAName = $ca.CAName Server = $ca.Server Subject = $ca.Subject Issuer = $ca.Issuer IsRootCA = $ca.IsSelfSigned HasTemplates = $ca.HasTemplates ParentCA = if ($parent) { $parent.CAName } else { $null } } } # Check online/offline (simple ping) $servers = $hierarchy.Server | Sort-Object -Unique $status = foreach ($s in $servers) { $online = Test-Connection -ComputerName $s -Count 1 -Quiet -ErrorAction SilentlyContinue [PSCustomObject]@{ Server = $s Online = $online } } # Join status back into hierarchy $final = $hierarchy | ForEach-Object { $caServer = $_.Server $st = $status | Where-Object { $_.Server -eq $caServer } | Select-Object -First 1 [PSCustomObject]@{ CAName = $_.CAName Server = $_.Server Online = $st.Online IsRootCA = $_.IsRootCA HasTemplates = $_.HasTemplates ParentCA = $_.ParentCA Subject = $_.Subject Issuer = $_.Issuer } } # Nice overview Write-AS2GoOutput -Type H1 -Message "Identitfied Enterprise CAs [$($final.count)]" if ($null -ne $final) { $final | Sort-Object IsRootCA, CAName | Format-Table CAName, Server, Online, HasTemplates, IsRootCA, ParentCA if (-not $InventoryOnly) { $Script:ADCSEnterpriseCAMissing = $false } } else { Write-AS2GoOutput -Type Info -Message "No Enterprise CA found." } $4logfile = $final | Select-Object CAName, Server, Online, HasTemplates, IsRootCA, ParentCA | Sort-Object IsRootCA, CAName | Format-Table | Out-String Write-Log -Message " >> Identified the following Get Enterprise CAs under Forest '$ConfigNC': $4logfile" Measure-FunctionRuntime -End -Details $Info } |