Convert-ADName.ps1

Function Convert-ADName
{
       [CmdletBinding()]  
    Param
    (
        [ValidateSet("Domain","Server","GC")]  
        [String]$InitType = "GC",
        [ValidateSet("DN","Canonical","NT4","Display","Domain","Enterprise","GUID","UPN","CanonicalEx","SPN","SIDHistory","SID")]   
        [String]$OutputType = "NT4",
        [String]$UserName,
        [String]$SID,
        [Int]$ID,
        [String]$Domain,
        [PSCredential]$Credential
    )

    Begin {}

    Process
    {
        Write-Debug "Start: Convert-UserName"

        $Result = $null
        if($UserName)
        {
            $InitTypeVal = switch($InitType)
            {
                "Domain" { 1; break; }
                "Server" { 2; break; }
                "GC"     { 3; break; }
            }

            #Name Transator Name Types
            $DISTINGUISHEDNAME     = 1
            $CANONICALNAME         = 2
            $NT4NAME               = 3
            $DISPLAYNAME           = 4
            $DOMAINSIMPLE          = 5
            $ENTERPRISESIMPLE      = 6
            $GUID                  = 7
            $UNKNOWN               = 8
            $USERPRINCIPALNAME     = 9
            $CANONICALEX          = 10
            $SERVICEPRINCIPALNAME = 11
            $SIDORSIDHISTORY      = 12

            $NameTranslate = New-Object -ComObject NameTranslate
            Try
            {
                if($Domain)
                {
                    $Cred = $Credential.GetNetworkCredential() 
                    $ExtParam = ( 
                        1, 
                        $Domain, 
                        $Cred.UserName, 
                        $Cred.Domain, 
                        $Cred.Password 
                    )
                    $Method = "InitEx" 
                } else
                {
                    $ExtParam = ($InitTypeVal,$null)
                    $Method = "Init" 
                }
                
                
                [System.__ComObject].InvokeMember($Method,"InvokeMethod",$null,$NameTranslate,$ExtParam) | Out-Null
                [System.__ComObject].InvokeMember("Set","InvokeMethod",$null,$NameTranslate,($UNKNOWN,$UserName)) | Out-Null
            }
            Catch
            {
                Write-Error $_.Exception
                Return
            }

            if($ID)
            {
                $Result = [System.__ComObject].InvokeMember("Get","InvokeMethod",$null,$NameTranslate,$ID)
             }
            else
            {
                $Result = switch($OutputType)
                {
                    "DN" { [System.__ComObject].InvokeMember("Get","InvokeMethod",$null,$NameTranslate,$DISTINGUISHEDNAME); break; }
                    "Canonical" { [System.__ComObject].InvokeMember("Get","InvokeMethod",$null,$NameTranslate,$CANONICALNAME); break; }
                    "NT4" { [System.__ComObject].InvokeMember("Get","InvokeMethod",$null,$NameTranslate,$NT4NAME); break; }
                    "Display" { [System.__ComObject].InvokeMember("Get","InvokeMethod",$null,$NameTranslate,$DISPLAYNAME); break; }
                    "Domain" { [System.__ComObject].InvokeMember("Get","InvokeMethod",$null,$NameTranslate,$DOMAINSIMPLE); break; }
                    "Enterprise" { [System.__ComObject].InvokeMember("Get","InvokeMethod",$null,$NameTranslate,$ENTERPRISESIMPLE); break; }
                    "Guid" { [System.__ComObject].InvokeMember("Get","InvokeMethod",$null,$NameTranslate,$GUID); break; }
                    "UPN" { [System.__ComObject].InvokeMember("Get","InvokeMethod",$null,$NameTranslate,$USERPRINCIPALNAME); break; }
                    "CanonicalEx" { [System.__ComObject].InvokeMember("Get","InvokeMethod",$null,$NameTranslate,$CANONICALEX); break; }
                    "SPN" { [System.__ComObject].InvokeMember("Get","InvokeMethod",$null,$NameTranslate,$SERVICEPRINCIPALNAME); break; }
                    "SIDHistory" { [System.__ComObject].InvokeMember("Get","InvokeMethod",$null,$NameTranslate,$SIDORSIDHISTORY); break; }
                    "SID" { Try
                            {
                                $objUser = New-Object System.Security.Principal.NTAccount($UserName)
                                $strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
                                $strSID.Value
                            }
                            Catch
                            {}
                            break }
                }
            }
        } elseif($SID)
        {
            Try
            {
                $objSID = New-Object System.Security.Principal.SecurityIdentifier($SID)
                $objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
                $Result = $objUser.Value
            }
            Catch
            {}
        }

        Write-Debug "Stop: Get-Name"
        Return $Result
    }

    End {}
}