Last active
April 24, 2025 08:40
-
-
Save joegasper/3fafa5750261d96d5e6edf112414ae18 to your computer and use it in GitHub Desktop.
Convert between DistinguishedName and CanonicalName
This file contains hidden or 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 characters
#Updated ConvertFrom-DN to support container objects | |
function ConvertFrom-DN { | |
[cmdletbinding()] | |
param( | |
[Parameter(Mandatory, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)] | |
[ValidateNotNullOrEmpty()] | |
[string[]]$DistinguishedName | |
) | |
process { | |
foreach ($DN in $DistinguishedName) { | |
Write-Verbose $DN | |
$CanonNameSlug = '' | |
$DC = '' | |
foreach ( $item in ($DN.replace('\,', '~').split(','))) { | |
if ( $item -notmatch 'DC=') { | |
$CanonNameSlug = $item.Substring(3) + '/' + $CanonNameSlug | |
} | |
else { | |
$DC += $item.Replace('DC=', ''); $DC += '.' | |
} | |
} | |
$CanonicalName = $DC.Trim('.') + '/' + $CanonNameSlug.Replace('~', '\,').Trim('/') | |
[PSCustomObject]@{ | |
'CanonicalName' = $CanonicalName; | |
} | |
} | |
} | |
} | |
function ConvertFrom-CanonicalUser { | |
[cmdletbinding()] | |
param( | |
[Parameter(Mandatory, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)] | |
[ValidateNotNullOrEmpty()] | |
[string]$CanonicalName | |
) | |
process { | |
$obj = $CanonicalName.Split('/') | |
[string]$DN = 'CN=' + $obj[$obj.count - 1] | |
for ($i = $obj.count - 2; $i -ge 1; $i--) { $DN += ',OU=' + $obj[$i] } | |
$obj[0].split('.') | ForEach-Object { $DN += ',DC=' + $_ } | |
return $DN | |
} | |
} | |
function ConvertFrom-CanonicalOU { | |
[cmdletbinding()] | |
param( | |
[Parameter(Mandatory, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)] | |
[ValidateNotNullOrEmpty()] | |
[string]$CanonicalName | |
) | |
process { | |
$obj = $CanonicalName.Split('/') | |
[string]$DN = 'OU=' + $obj[$obj.count - 1] | |
for ($i = $obj.count - 2; $i -ge 1; $i--) { $DN += ',OU=' + $obj[$i] } | |
$obj[0].split('.') | ForEach-Object { $DN += ',DC=' + $_ } | |
return $DN | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you @shivtorov your latest update sent me down a rabbit hole wondering if there was a way to use what AD does to map names. With Pwsh making .NET calls possible, I came across DsCrackName and namespace calls like [System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain() right in Pwsh. This led me to a COM object called NameTranslate that had code examples! I started writing new Pwsh based off the examples, then got smarter and found this gist - seems to work with whatever I toss at it, including your last example.
Convert-ADName.ps1