Advertisement
Old-Lost

ConvertTo-DN

Feb 8th, 2017
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ConvertTo-DN {
  2.     <#
  3.         .SYNOPSIS
  4.         Convert a CanonicalName to a DistinguishedName
  5.         .DESCRIPTION
  6.         Takes an absolute CanonicalName value of an ActiveDirectory object and returns the corresponding DistinguishedName. The object must exist in AD.
  7.         .PARAMETER  CanonicalName
  8.         The CanonicalName value to examine. This can be supplied implicitly by piping an object that has a CanonicalName property.
  9.         .PARAMETER  InputObject
  10.         The object to manipulate. This parameter is typically passed to the function through the pipeline. If this parameter is supplied the -PropertyName parameter must also be specified.
  11.         .PARAMETER  PropertyName
  12.         The name of the property to add (as a NoteProperty) to the InputObject. This property will be set to the DistinguishedName value.
  13.         .EXAMPLE
  14.         'fabrikan.com/Users/Harden, Marsha Gay' | ConvertTo-DN
  15.         CN=Harden\, Marsha Gay,OU=Users,DC=fabrikan,DC=com
  16.         .EXAMPLE
  17.         $ADOrgUnitObject | ConvertTo-DN -Property DN
  18.         OU=TestOU,OU=Testing,DC=fabrikan,DC=com
  19.         .INPUTS
  20.         System.String, PSObject
  21.         .OUTPUTS
  22.         System.String, PSObject
  23.         .NOTES
  24.         Convertion is done by string manipulation alone, so the resulting DistinguishedName may or may not be valid.
  25.         One cause of mismatch is leading 'CN=' may need to change to 'OU=' if the object is an Organizational Unit.
  26.     #>
  27.     [CmdletBinding(DefaultParameterSetName = 'None')]
  28.     [Alias('ConvertTo-DistinguishedName', 'ConvertFrom-CanonicalName', 'ConvertFrom-CN')]
  29.     [OutputType([string], ParameterSetName = "None")]
  30.     [OutputType([PSObject], ParameterSetName = "InputObject")]
  31.     Param (
  32.         [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = 'None')]
  33.         [Alias('CN')][string[]]$CanonicalName,
  34.         [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'InputObject')][PSObject[]]$InputObject,
  35.         [Parameter(Mandatory, ParameterSetName = 'InputObject')][string]$PropertyName = 'DN'
  36.     )
  37.     begin {
  38.         [boolean]$PipelineInput = -not $PSBoundParameters.ContainsKey("CanonicalName")
  39.         $HexMap = @{}
  40.         1..31 | % { $HexMap[('\x' + $_.ToString('X2'))] = '\' + $_.ToString('X2') }
  41.         function CDN {
  42.             [CmdletBinding()]
  43.             Param ([Parameter(Position = 0, Mandatory)][string]$CN)
  44.             # first we split on backslash (but only if not escaped),
  45.             # then we escape those characters that need escaping in a DistinguishedName.
  46.             trap { return }
  47.             [System.Collections.ArrayList]$Obj = @(($CN -split '(?<!\\)/') | % { ($_ -replace '\\/', '/') -replace '(\A\s|(?<!\\)\\(?!\\)|[,#+<>;"=]|\s\z)', '\$1' })
  48.             # replace non-printable characters
  49.             foreach ($k in $HexMap.Keys) { $Obj = $Obj -replace $k, $HexMap[$k] }
  50.             [string]$DC = ',DC=' + (($Obj[0] -split '\.') -join ',DC=')
  51.             if ($Obj.Count) { [void]$Obj.RemoveAt(0); [void]$Obj.Reverse() }
  52.             [string]$DN = if ($Obj.Count) { 'CN=' + $Obj[0]; [void]$Obj.RemoveAt(0) } else { '' }
  53.             $DN += if ($Obj.Count) { ',OU=' + ($Obj -join ',OU=') + $DC } else { $DC }
  54.             $DN = $DN -replace ",OU=(Users|Computers|Deleted Objects|ForeignSecurityPrincipals|LostAndFound|System|Builtin|Microsoft Exchange System Objects),DC=", ',CN=$1,DC='
  55.             $DN -replace '\A,'
  56.         }
  57.     }
  58.     PROCESS {
  59.         if ($PipelineInput) {
  60.             if ($PSBoundParameters.ContainsKey('PropertyName')) {
  61.                 foreach ($o in $InputObject) {
  62.                     $DN = CDN -CN $o.CanonicalName
  63.                     $o | Add-Member -NotePropertyName $PropertyName -NotePropertyValue $DN -Force -PassThru
  64.                 }
  65.             } else {
  66.                 CDN -CN $CanonicalName[0]
  67.             }
  68.         } else {
  69.             foreach ($CN in $CanonicalName) {
  70.                 CDN -CN $CN
  71.             }
  72.         }
  73.     }
  74. } # ConvertTo-DN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement