Get-NormalizedO365.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<#
    .SYNOPSIS
 
    This function is used to normalize the DN information of users on premises to SMTP addresses utilized in Office 365.
 
    .DESCRIPTION
 
    This function is used to normalize the DN information of users on premises to SMTP addresses utilized in Office 365.
 
    .PARAMETER GlobalCatalog
 
    The global catalog to make the query against.
 
    .PARAMETER DN
 
    The DN of the object to pass to normalize.
 
    .PARAMETER CN
 
    THe canonical name of an object to normalize.
 
    .PARAMETER adCredential
 
    The AD credential for global catalog connections.
 
    .PARAMETER originalGroupDN
 
    The DN of the original group on premises.
 
    .PARAMETER isMember
 
    Boolean if the object to be tested is a member.
 
    .OUTPUTS
 
    Selects the mail address of the user by DN and returns the mail address.
 
    .EXAMPLE
 
    get-normalizedDN -globalCatalog GC -DN DN -adCredential CRED -isMember FALSE
 
    #>

    Function Get-NormalizedO365
     {
        [cmdletbinding()]

        Param
        (
            [Parameter(Mandatory = $true)]
            $attributeToNormalize
        )

        #Output all parameters bound or unbound and their associated values.

        write-functionParameters -keyArray $MyInvocation.MyCommand.Parameters.Keys -parameterArray $PSBoundParameters -variableArray (Get-Variable -Scope Local -ErrorAction Ignore)

        #Funtion variables.

        $functionRecipient = $null
        $functionObject = $null
        $functionReturnArray = @()

        #Start function processing.

        Out-LogFile -string "********************************************************************************"
        Out-LogFile -string "BEGIN GET-NormalizedO365"
        Out-LogFile -string "********************************************************************************"
        
        #Get the specific user using ad providers.

        out-logfile -string "Determine if attribute has values to convert."

        if ($attributeToNormalize.count -gt 0)
        {
            out-logfile -string "Attribute to convert has values."

            foreach ($member in $attributeToNormalize)
            {
                if ($member -ne "Organization Management")
                {
                    out-logfile -string ("Testing member: "+$member)

                    try {
                        out-logfile -string "Testing for recipient type."

                        $functionRecipient = get-o365Recipient -identity $member -errorAction STOP

                        if ($functionRecipient.count -gt 0)
                        {
                            out-logfile -string "The attribute to be normalized only contains names. The name resulted in more than one object being returned via get-recipient."

                            foreach ($object in $functionRecipient)
                            {
                                $functionObject = New-Object PSObject -Property @{
                                    DisplayName = $object.displayName
                                    PrimarySMTPAddressOrUPN = $object.primarySMTPAddress
                                    ExternalDirectoryObjectID = ("User_"+$object.externalDirectoryObjectID)
                                    isError=$NULL
                                    isErrorMessage=$null
                                    isAmbiguous=$TRUE 
                                }

                                out-logfile -string $functionObject  

                                $functionReturnArray += $functionObject
                            }
                        }
                        else {
                            out-logfile -string "Only a single object was found - not ambiguous."

                            $functionObject = New-Object PSObject -Property @{
                                DisplayName = $functionRecipient.displayName
                                PrimarySMTPAddressOrUPN = $functionRecipient.primarySMTPAddress
                                ExternalDirectoryObjectID = ("User_"+$functionRecipient.externalDirectoryObjectID)
                                isError=$NULL
                                isErrorMessage=$null
                                isAmbiguous=$false
                            }

                            out-logfile -string $functionObject

                            $functionReturnArray += $functionObject
                        }
                    }
                    catch {

                        out-logfile -string $_
                        out-logfile -string "Testing for recipient type failed."

                        try {

                            out-logfile -string "Testing object for user type."

                            $functionRecipient = get-o365user -identity $member -errorAction STOP

                            if ($functionRecipient.count -gt 0)
                            {
                                out-logfile -string "Multiple users were found on ambiguous query."

                                foreach ($object in $functionRecipient)
                                {
                                    $functionObject = New-Object PSObject -Property @{
                                        DisplayName = $object.DisplayName
                                        PrimarySMTPAddressOrUPN = $object.UserPrincipalName
                                        ExternalDirectoryObjectID = ("User_"+$object.externalDirectoryObjectID)
                                        isError=$NULL
                                        isErrorMessage=$null
                                        isAmbiguous=$true
                                    }

                                    out-logfile -string $functionObject  

                                    $functionReturnArray += $functionObject
                                }
                            }
                            else {
                                out-logfile -string "Only single user was returned / not ambiguous."
                                
                                $functionObject = New-Object PSObject -Property @{
                                    DisplayName = $functionRecipient.DisplayName
                                    PrimarySMTPAddressOrUPN = $functionRecipient.UserPrincipalName
                                    ExternalDirectoryObjectID = ("User_"+$functionRecipient.externalDirectoryObjectID)
                                    isError=$NULL
                                    isErrorMessage=$null
                                    isAmbiguous=$false
                                }

                                out-logfile -string $functionObject  

                                $functionReturnArray += $functionObject
                            }
                        }
                        catch {
                            out-logfile -string $_
                            out-logfile -string "A user or recipient in the group cannot be located." -isError:$TRUE
                        }
                    }
                }
                else {
                    out-logfile -string "Member is the organization management built in role group - skip."
                }
            }
        }
        else 
        {
            out-logfile -string "No values to normalize were provided."
            $functionReturnArray = @()
        }

        Out-LogFile -string "END GET-NormalizedO365"
        Out-LogFile -string "********************************************************************************"
        
        return $functionReturnArray
    }