Advertisement
Old-Lost

ConvertTo-CN

Feb 6th, 2017
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ConvertTo-CN {
  2.     <#
  3.     .SYNOPSIS
  4.         Converts a DistinguishedName to its equivalent CanonicalName format.
  5.  
  6.     .DESCRIPTION
  7.         Converts a DistinguishedName value to its corresponding CannonicalName value.
  8.  
  9.     .PARAMETER  DistinguishedName
  10.         The DistinguishedName value to examine. This can be supplied implicitly by piping an object that has a DistinguishedName property.
  11.  
  12.     .PARAMETER  InputObject
  13.         The object(s) to process. This parameter is typically supplied by piping objects to the function.
  14.  
  15.     .PARAMETER  PropertyName
  16.         The name of the property to add (as a NoteProperty) to the InputObject. This property will be set to the CanonicalName value calculated.
  17.  
  18.     .EXAMPLE
  19.         'CN=Harden\, Marsha Gay,OU=Users,DC=fabrikan,DC=com' | ConvertTo-CN
  20.       fabrikan.com/Users/Harden, Marsha Gay
  21.  
  22.     .EXAMPLE
  23.         $ADOrgUnitObject | ConvertTo-CN -PropertyName CN
  24.       OU=TestOU,OU=Testing,DC=fabrikan,DC=com
  25.  
  26.     .INPUTS
  27.         System.String, PSObject
  28.  
  29.     .OUTPUTS
  30.         System.String, PSObject
  31.  
  32.     .NOTES
  33.         The CanonicalName value is calculated using only string manipulation; ActiveDirectory is not checked so the object to which the DistinguishedName refers need not exist.
  34.  
  35.       #>
  36.     [CmdletBinding(DefaultParameterSetName = 'None')]
  37.     [Alias('ConvertTo-CanonicalName', 'ConvertFrom-DN', 'ConvertFrom-DistinguishedName')]
  38.     [OutputType([string], ParameterSetName = "None")]
  39.     [OutputType([PSObject], ParameterSetName = "InputObject")]
  40.     Param (
  41.         [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = 'None')][Alias('DN')][string[]]$DistinguishedName,
  42.         [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'InputObject')][PSObject[]]$InputObject,
  43.         [Parameter(Mandatory, ParameterSetName = 'InputObject')][string]$PropertyName = 'DN'
  44.     )
  45.     begin {
  46.         # Write-Host ($PSBoundParameters | Out-String); Write-Host $PSCmdlet.ParameterSetName
  47.         [boolean]$PipelineInput = -not $PSBoundParameters.ContainsKey("DistinguishedName")
  48.         function CCN {
  49.             [CmdletBinding()]
  50.             Param ([Parameter(Position = 0, Mandatory)][string]$DN)
  51.             [System.Collections.ArrayList]$CN = @(); [System.Collections.ArrayList]$DC = @()
  52.             [System.Collections.ArrayList]$Items = @((($DN -replace '\\,', '##COMMA##') -split ',') | % {
  53.                     $_ = $_ -replace '\\([,\#+<>;"=])', '$1'
  54.                     $_ = $_ -replace '\A\\\s|\\\s\z', ' '
  55.                     $_ = $_ -replace '([\/])', '\$1'
  56.                     while ($_ -match '(.*?)(?<!\\)\\([a-f\d]{2})(.*)') {
  57.                         $_ = $Matches[1] + ([CHAR][CONVERT]::ToInt16($Matches[2], 16)) + $Matches[3]
  58.                     }
  59.                     $_
  60.                 })
  61.             [void]$Items.Reverse()
  62.             while (($Items.Count -gt 0) -and ($Items[0] -match '\ADC=')) {
  63.                 [void]$DC.Add(($Items[0] -replace '\ADC='))
  64.                 [void]$Items.RemoveAt(0)
  65.             }
  66.             [void]$DC.Reverse()
  67.             while ($Items.Count -gt 0) {
  68.                 [void]$CN.Add(($Items[0] -replace '\A(DC|OU|CN)='))
  69.                 [void]$Items.RemoveAt(0)
  70.             }
  71.             (($DC -join '.') + '/' + (($CN -join '/') -replace '##COMMA##', ','))
  72.         }
  73.     }
  74.     process {
  75.         # Write-Host ($PSBoundParameters | Out-String); Write-Host $PSCmdlet.ParameterSetName
  76.         if ($PipelineInput) {
  77.             if ($PSBoundParameters.ContainsKey('PropertyName')) {
  78.                 $CN = CCN -DN $InputObject.DistinguishedName
  79.                 $InputObject[0] | Add-Member -NotePropertyName $PropertyName -NotePropertyValue $CN -Force -PassThru
  80.             } else {
  81.                 CCN -DN $DistinguishedName[0]
  82.             }
  83.         } else {
  84.             foreach ($DN in $DistinguishedName) {
  85.                 CCN -DN $DN
  86.             }
  87.         }
  88.     }
  89. } # ConvertTo-CN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement