Convert-ADName.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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 {}
}