Created
July 16, 2019 09:42
-
-
Save indented-automation/17d57f0476b352cbbe8903b4c78d74bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum NameType { | |
DistinguishedName = 1 | |
CanonicalName | |
NT4 | |
DisplayName | |
DomainSimple | |
EnterpriseSimple | |
Guid | |
Unknown | |
UserPrincipalName | |
CanonicalNameExtended | |
ServicePrincipalName | |
SidOrSidHistory | |
} | |
enum InitType { | |
Domain = 1 | |
Server | |
GlobalCatalog | |
} | |
function Convert-ADName { | |
<# | |
.SYNOPSIS | |
Uses the NameTranslate interface to convert between different name formats. | |
.DESCRIPTION | |
Uses the NameTranslate interface to convert between different name formats. | |
.EXAMPLE | |
Convert-ADName 'First Last' -From DisplayName -To CanonicalName | |
Converts the display name "First Last" to a canonical name. | |
.EXAMPLE | |
Convert-ADName -Identity "domain.com/Users`nFirst Last" | |
Convert the extended canonical name for First Last to a distinguished name. | |
.EXAMPLE | |
Convert-ADName -Identity 'domain.com/ou1/ou2/name' | |
Convert the canonical name to a user principal name. | |
#> | |
[CmdletBinding()] | |
param ( | |
# The identity to convert. | |
[Parameter(Mandatory, Position = 1, ValueFromPipeline, ValueFromPipelineByPropertyName)] | |
[String]$Identity, | |
# The source type for the name. | |
[NameType]$From = 'Unknown', | |
# The destination type for the name. | |
[NameType]$To = 'DistinguishedName', | |
# The initialization type. If the InitType is Domain, the ComputerName parameter must use the domain name. | |
[InitType]$InitType = 'GlobalCatalog', | |
# The server to use to perform the translation operation. By default the server is auto-discovered by the NameTranslate interface. | |
[String]$ComputerName, | |
# Credentials to use for this operation. | |
[PSCredential]$Credential | |
) | |
begin { | |
if ($To -in 'Unknown', 'ServicePrincipalName', 'SidOrSidHistory') { | |
$errorRecord = [System.Management.Automation.ErrorRecord]::new( | |
[ArgumentException]::new('The destination name type is invalid'), | |
'InvalidDestinationNameType', | |
'InvalidArgument', | |
$To | |
) | |
$pscmdlet.ThrowTerminatingError($errorRecord) | |
} | |
try { | |
$nameTranslate = New-Object -ComObject NameTranslate | |
if ($Credential) { | |
$networkCredential = $Credential.GetNetworkCredential() | |
$nameTranslate.InitEx( | |
[Int64]$InitType, | |
$ComputerName, | |
$networkCredential.Username, | |
$networkCredential.Domain, | |
$networkCredential.Password | |
) | |
} else { | |
$nameTranslate.Init( | |
[Int64]$InitType, | |
$ComputerName | |
) | |
} | |
} catch { | |
$pscmdlet.ThrowTerminatingError($_) | |
} | |
} | |
process { | |
try { | |
$nameTranslate.Set($From, $Identity) | |
$nameTranslate.Get($To) | |
} catch { | |
Write-Error -ErrorRecord $_ | |
} | |
} | |
end { | |
$null = [System.Runtime.InteropServices.Marshal]::ReleaseComObject($nameTranslate) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment