Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ConvertTo-CN {
- <#
- .SYNOPSIS
- Converts a DistinguishedName to its equivalent CanonicalName format.
- .DESCRIPTION
- Converts a DistinguishedName value to its corresponding CannonicalName value.
- .PARAMETER DistinguishedName
- The DistinguishedName value to examine. This can be supplied implicitly by piping an object that has a DistinguishedName property.
- .PARAMETER InputObject
- The object(s) to process. This parameter is typically supplied by piping objects to the function.
- .PARAMETER PropertyName
- The name of the property to add (as a NoteProperty) to the InputObject. This property will be set to the CanonicalName value calculated.
- .EXAMPLE
- 'CN=Harden\, Marsha Gay,OU=Users,DC=fabrikan,DC=com' | ConvertTo-CN
- fabrikan.com/Users/Harden, Marsha Gay
- .EXAMPLE
- $ADOrgUnitObject | ConvertTo-CN -PropertyName CN
- OU=TestOU,OU=Testing,DC=fabrikan,DC=com
- .INPUTS
- System.String, PSObject
- .OUTPUTS
- System.String, PSObject
- .NOTES
- The CanonicalName value is calculated using only string manipulation; ActiveDirectory is not checked so the object to which the DistinguishedName refers need not exist.
- #>
- [CmdletBinding(DefaultParameterSetName = 'None')]
- [Alias('ConvertTo-CanonicalName', 'ConvertFrom-DN', 'ConvertFrom-DistinguishedName')]
- [OutputType([string], ParameterSetName = "None")]
- [OutputType([PSObject], ParameterSetName = "InputObject")]
- Param (
- [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = 'None')][Alias('DN')][string[]]$DistinguishedName,
- [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'InputObject')][PSObject[]]$InputObject,
- [Parameter(Mandatory, ParameterSetName = 'InputObject')][string]$PropertyName = 'DN'
- )
- begin {
- # Write-Host ($PSBoundParameters | Out-String); Write-Host $PSCmdlet.ParameterSetName
- [boolean]$PipelineInput = -not $PSBoundParameters.ContainsKey("DistinguishedName")
- function CCN {
- [CmdletBinding()]
- Param ([Parameter(Position = 0, Mandatory)][string]$DN)
- [System.Collections.ArrayList]$CN = @(); [System.Collections.ArrayList]$DC = @()
- [System.Collections.ArrayList]$Items = @((($DN -replace '\\,', '##COMMA##') -split ',') | % {
- $_ = $_ -replace '\\([,\#+<>;"=])', '$1'
- $_ = $_ -replace '\A\\\s|\\\s\z', ' '
- $_ = $_ -replace '([\/])', '\$1'
- while ($_ -match '(.*?)(?<!\\)\\([a-f\d]{2})(.*)') {
- $_ = $Matches[1] + ([CHAR][CONVERT]::ToInt16($Matches[2], 16)) + $Matches[3]
- }
- $_
- })
- [void]$Items.Reverse()
- while (($Items.Count -gt 0) -and ($Items[0] -match '\ADC=')) {
- [void]$DC.Add(($Items[0] -replace '\ADC='))
- [void]$Items.RemoveAt(0)
- }
- [void]$DC.Reverse()
- while ($Items.Count -gt 0) {
- [void]$CN.Add(($Items[0] -replace '\A(DC|OU|CN)='))
- [void]$Items.RemoveAt(0)
- }
- (($DC -join '.') + '/' + (($CN -join '/') -replace '##COMMA##', ','))
- }
- }
- process {
- # Write-Host ($PSBoundParameters | Out-String); Write-Host $PSCmdlet.ParameterSetName
- if ($PipelineInput) {
- if ($PSBoundParameters.ContainsKey('PropertyName')) {
- $CN = CCN -DN $InputObject.DistinguishedName
- $InputObject[0] | Add-Member -NotePropertyName $PropertyName -NotePropertyValue $CN -Force -PassThru
- } else {
- CCN -DN $DistinguishedName[0]
- }
- } else {
- foreach ($DN in $DistinguishedName) {
- CCN -DN $DN
- }
- }
- }
- } # ConvertTo-CN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement