Test-O365Recipient.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
<#
    .SYNOPSIS
 
    This function tests if the recipient is found in Office 365.
 
    .DESCRIPTION
 
    This function tests to ensure a recipient is found in Office 365.
 
    .PARAMETER member
 
    The member to test for.
 
    .OUTPUTS
 
    None
 
    .EXAMPLE
 
    test-o365Recipient -member $member
 
    #>

    Function Test-O365Recipient
     {
        [cmdletbinding()]

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

        out-logfile -string "Output bound parameters..."

        #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)

        #Declare local variables.

        [array]$functionDirectoryObjectID=@()
        [string]$isTestError="No"

        #Start function processing.

        Out-LogFile -string "********************************************************************************"
        Out-LogFile -string "BEGIN TEST-O365RECIPIENT"
        Out-LogFile -string "********************************************************************************"

        if (($member.externalDirectoryObjectID -ne $NULL) -and ($member.recipientOrUser -eq "Recipient"))
        {
            out-LogFile -string "Testing based on External Directory Object ID"
            out-logfile -string $member.ExternalDirectoryObjectID
            out-logfile -string $member.recipientOrUser

            #Modify the external directory object id.

            $functionDirectoryObjectID=$member.externalDirectoryObjectID.Split("_")

            out-logfile -string $functionDirectoryObjectID[1]

            try {
                #get-exoRecipient -identity $functionDirectoryObjectID[1] -errorAction STOP
                get-o365Recipient -identity $functionDirectoryObjectID[1] -errorAction STOP
                $isTestError="No"
            }
            catch {
                out-logfile -string ("The recipient was not found in Office 365. ERROR --"+$functionDirectoryObjectID[1] )
                out-logFile -string $_
                $isTestError="Yes"
            }
        }
        elseif (($member.PrimarySMTPAddressOrUPN -ne $NULL) -and ($member.recipientoruser -eq "Recipient"))
        {
            out-LogFile -string "Testing based on Primary SMTP Address"
            out-logfile -string $member.PrimarySMTPAddressOrUPN
            out-logfile -string $member.recipientOrUser

            try {
                #get-exoRecipient -identity $member.PrimarySMTPAddressOrUPN -errorAction Stop
                get-o365Recipient -identity $member.PrimarySMTPAddressOrUPN -errorAction Stop
                $isTestError="No"
            }
            catch {
                out-logfile -string ("The recipient was not found in Office 365. ERROR -- "+$member.primarySMTPAddressOrUPN)
                out-logfile -string $_
                $isTestError="Yes"
            }
        }
        elseif (($member.ExternalDirectoryObjectID -ne $NULL) -and ($member.recipientoruser -eq "User"))
        {
            out-LogFile -string "Testing based on external directory object ID."
            out-logfile -string $member.externalDirectoryObjectID
            out-logfile -string $member.recipientOrUser

            #Modify the external directory object id.

            $functionDirectoryObjectID=$member.externalDirectoryObjectID.Split("_")

            out-logfile -string $functionDirectoryObjectID[1]

            try {
                get-o365User -identity $functionDirectoryObjectID[1] -errorAction STOP
                $isTestError="No"
            }
            catch {
                out-logfile -string ("The recipient was not found in Office 365. ERROR --"+$functionDirectoryObjectID[1] )
                out-logFile -string $_
                $isTestError="Yes"
            }
        }
        elseif (($member.PrimarySMTPAddressOrUPN -ne $NULL) -and ($member.recipientoruser -eq "User"))
        {
            out-LogFile -string "Testing based on user principal name."
            out-logfile -string $member.PrimarySMTPAddressOrUPN
            out-logfile -string $member.recipientOrUser

            try {
                get-o365User -identity $member.primarySMTPAddressOrUPN -errorAction STOP
                $isTestError="No"
            }
            catch {
                out-logfile -string ("The recipient was not found in Office 365. ERROR -- "+$member.primarySMTPAddressOrUPN)
                out-logfile -string $_
                $isTestError="Yes"
            }
        }
        elseif ($member.recipientOrUser -eq "SecurityGroup")
        {
            out-logfile -string "Found a security group as a member."
            out-logfile -string "Testing based on GUID which was overloaded with the groups SID."
            out-logfile -string ("Testing SID: "+$member.GUID)

            try
            {
                $functionFilter = "`""
                $functionFilter += "onPremisesSecurityIdentifier eq "
                $functionFilter += "`'"
                $functionFilter += $member.GUID
                $functionFilter += "`'"
                $functionFilter += "`""
                out-logfile -string $functionFilter

                $functionCommand = "get-azureADGroup -filter $functionFilter"

                out-logfile -string $functionCommand

                $scriptBlock=[scriptBlock]::create($functionCommand)

                $functionTest = invoke-command -ScriptBlock $scriptBlock

                $member.externalDirectoryObjectID = ("User_"+$functionTest.ObjectId)
                $member.alias = $functionTest.mailNickName
            }
            catch
            {
                out-logfile -string ("The recipient was not found in Office 365. ERROR -- "+$member.GUID)
                out-logfile -string $_
                $isTestError="Yes"
            }
        }
        else 
        {
            out-logfile -string "An invalid object was passed to test-o365recipient - failing."
            $isTestError="Yes"
        }

        Out-LogFile -string "END TEST-O365RECIPIENT"
        Out-LogFile -string "********************************************************************************"    

        return $isTestError
    }