PoshCode Archive  Artifact [57616220fc]

Artifact 57616220fc3119710ec9436acd3178de53c3d063d62cc68adf358e75db681e0c:

  • File DN-to-Canonical.ps1 — part of check-in [a76cece229] at 2018-06-10 14:04:55 on branch trunk — Not all objects which looks like OrganizationalUnit are them – some are Containters (e.g. User, Computers) – function corrected and extended to handle this kind of situations (user: Andrew size: 1531)

# encoding: ascii
# api: powershell
# title: DN to Canonical
# description: Not all objects which looks like OrganizationalUnit are them – some are Containters (e.g. User, Computers) – function corrected and extended to handle this kind of situations
# version: 0.1
# type: function
# author: Andrew
# license: CC0
# function: ConvertFrom-DN
# x-poshcode-id: 5960
# x-archived: 2016-05-17T13:25:45
# x-published: 2016-08-01T20:27:00
#
#
function ConvertFrom-DN {
    
    
    #Based on: http://practical-admin.com/blog/convert-dn-to-canoincal-and-back/
    #Author: Andrew
    #Corrected by Wojciech Sciesinski wojciech[at]sciesinski[dot]net
    
    param ([string]$DN = (Throw '$DN is required!'))
    
    foreach ($item in ($DN.replace('\,', '~').split(","))) {
        
        switch -regex ($item.TrimStart().Substring(0, 3)) {
            
            "CN=" { $CN +=, $item.replace("CN=", ""); $CN += '/'; continue }
            
            "OU=" { $OU +=, $item.replace("OU=", ""); $OU += '/'; continue }
            
            "DC=" { $DC += $item.replace("DC=", ""); $DC += '.'; continue }
            
        }
        
    }
    
    $canonical = $dc.Substring(0, $dc.length - 1)
    
    If ($ou.count -gt 0) {
        
	for ($i = $ou.count; $i -ge 0; $i--) { $canonical += $ou[$i] }
        
    }
    
    If ($CN.count -gt 0) {
        
        for ($i = $CN.count; $i -ge 0; $i--) { $canonical += $CN[$i] }
        
    }
    
    return $canonical
}