src/Private/ConvertTo-DomainDns.ps1
|
function ConvertTo-DomainDns { <# .SYNOPSIS Derives a domain DNS name from a distinguished name's DC= components. .DESCRIPTION Pulls the trailing domain-component (DC=) labels off a distinguished name and joins them with dots, e.g. 'OU=Users,DC=na,DC=contoso,DC=com' -> 'na.contoso.com'. Any leading OU=/CN= components are ignored, so it works on both a naming-context root and an OU base. Only DC= at an RDN boundary (string start or just after a comma) is treated as a domain component - a literal 'DC=' inside a CN/OU value (legal unescaped per RFC 4514) is not mistaken for one. Returns $null when no DC= component is present (so callers can leave Domain unset rather than emit a bogus value). #> [CmdletBinding()] param( [Parameter(ValueFromPipeline)] [string] $DistinguishedName ) process { if (-not $DistinguishedName) { return $null } $labels = [regex]::Matches($DistinguishedName, '(?i)(?:^|,)\s*DC=([^,]+)') | ForEach-Object { $_.Groups[1].Value } if (-not $labels -or $labels.Count -eq 0) { return $null } return ($labels -join '.') } } |