Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ConvertTo-DN {
- <#
- .SYNOPSIS
- Convert a CanonicalName to a DistinguishedName
- .DESCRIPTION
- Takes an absolute CanonicalName value of an ActiveDirectory object and returns the corresponding DistinguishedName. The object must exist in AD.
- .PARAMETER CanonicalName
- The CanonicalName value to examine. This can be supplied implicitly by piping an object that has a CanonicalName property.
- .PARAMETER InputObject
- 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.
- .PARAMETER PropertyName
- The name of the property to add (as a NoteProperty) to the InputObject. This property will be set to the DistinguishedName value.
- .EXAMPLE
- 'fabrikan.com/Users/Harden, Marsha Gay' | ConvertTo-DN
- CN=Harden\, Marsha Gay,OU=Users,DC=fabrikan,DC=com
- .EXAMPLE
- $ADOrgUnitObject | ConvertTo-DN -Property DN
- OU=TestOU,OU=Testing,DC=fabrikan,DC=com
- .INPUTS
- System.String, PSObject
- .OUTPUTS
- System.String, PSObject
- .NOTES
- Convertion is done by string manipulation alone, so the resulting DistinguishedName may or may not be valid.
- One cause of mismatch is leading 'CN=' may need to change to 'OU=' if the object is an Organizational Unit.
- #>
- [CmdletBinding(DefaultParameterSetName = 'None')]
- [Alias('ConvertTo-DistinguishedName', 'ConvertFrom-CanonicalName', 'ConvertFrom-CN')]
- [OutputType([string], ParameterSetName = "None")]
- [OutputType([PSObject], ParameterSetName = "InputObject")]
- Param (
- [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = 'None')]
- [Alias('CN')][string[]]$CanonicalName,
- [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'InputObject')][PSObject[]]$InputObject,
- [Parameter(Mandatory, ParameterSetName = 'InputObject')][string]$PropertyName = 'DN'
- )
- begin {
- [boolean]$PipelineInput = -not $PSBoundParameters.ContainsKey("CanonicalName")
- $HexMap = @{}
- 1..31 | % { $HexMap[('\x' + $_.ToString('X2'))] = '\' + $_.ToString('X2') }
- function CDN {
- [CmdletBinding()]
- Param ([Parameter(Position = 0, Mandatory)][string]$CN)
- # first we split on backslash (but only if not escaped),
- # then we escape those characters that need escaping in a DistinguishedName.
- trap { return }
- [System.Collections.ArrayList]$Obj = @(($CN -split '(?<!\\)/') | % { ($_ -replace '\\/', '/') -replace '(\A\s|(?<!\\)\\(?!\\)|[,#+<>;"=]|\s\z)', '\$1' })
- # replace non-printable characters
- foreach ($k in $HexMap.Keys) { $Obj = $Obj -replace $k, $HexMap[$k] }
- [string]$DC = ',DC=' + (($Obj[0] -split '\.') -join ',DC=')
- if ($Obj.Count) { [void]$Obj.RemoveAt(0); [void]$Obj.Reverse() }
- [string]$DN = if ($Obj.Count) { 'CN=' + $Obj[0]; [void]$Obj.RemoveAt(0) } else { '' }
- $DN += if ($Obj.Count) { ',OU=' + ($Obj -join ',OU=') + $DC } else { $DC }
- $DN = $DN -replace ",OU=(Users|Computers|Deleted Objects|ForeignSecurityPrincipals|LostAndFound|System|Builtin|Microsoft Exchange System Objects),DC=", ',CN=$1,DC='
- $DN -replace '\A,'
- }
- }
- PROCESS {
- if ($PipelineInput) {
- if ($PSBoundParameters.ContainsKey('PropertyName')) {
- foreach ($o in $InputObject) {
- $DN = CDN -CN $o.CanonicalName
- $o | Add-Member -NotePropertyName $PropertyName -NotePropertyValue $DN -Force -PassThru
- }
- } else {
- CDN -CN $CanonicalName[0]
- }
- } else {
- foreach ($CN in $CanonicalName) {
- CDN -CN $CN
- }
- }
- }
- } # ConvertTo-DN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement