Get-O365DLMembership.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
<#
    .SYNOPSIS
 
    This function obtains the DL membership of the Office 365 distribution group.
 
    .DESCRIPTION
 
    This function obtains the DL membership of the Office 365 distribution group.
 
    .PARAMETER GroupSMTPAddress
 
    The mail attribute of the group to search.
 
    .OUTPUTS
 
    Returns the membership array of the DL in Office 365.
 
    .EXAMPLE
 
    get-o365dlMembership -groupSMTPAddress Address
 
    #>

    Function Get-o365DLMembership
     {
        [cmdletbinding()]

        Param
        (
            [Parameter(Mandatory = $true)]
            [string]$groupSMTPAddress,
            [Parameter(Mandatory = $false)]
            [boolean]$isUnifiedGroup=$false,
            [Parameter(Mandatory = $false)]
            [boolean]$getUnifiedMembers=$false,
            [Parameter(Mandatory = $false)]
            [boolean]$getUnifiedOwners=$false,
            [Parameter(Mandatory = $false)]
            [boolean]$getUnifiedSubscribers=$false,
            [Parameter(Mandatory = $false)]
            [boolean]$isHealthReport=$false
        )

        #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 function variables.

        $functionDLMembership=$NULL #Holds the return information for the group query.
        $functionMembersLinkType = "Members"
        $functionOwnersLinkType = "Owners"
        $functionSubscribersLinkType = "Subscribers"

        #Start function processing.

        Out-LogFile -string "********************************************************************************"
        Out-LogFile -string "BEGIN GET-O365DLMEMBERSHIP"
        Out-LogFile -string "********************************************************************************"

        #Get the recipient using the exchange online powershell session.

        if ($isUnifiedGroup -eq $FALSE)
        {
            if ($isHealthReport -eq $FALSE)
            {
                Out-LogFile -string "Using Exchange Online to obtain the group membership."

                $functionDLMembership=@(get-O365DistributionGroupMember -identity $groupSMTPAddress -resultsize unlimited -errorAction STOP)
                
                Out-LogFile -string "Distribution group membership recorded."
            }
            else 
            {
                Out-LogFile -string "Using Exchange Online to obtain the group membership."

                $functionDLMembership=@(get-O365DistributionGroupMember -identity $groupSMTPAddress -resultsize unlimited -errorAction STOP | select-object Identity,Name,Alias,ExternalDirectoryObjectId,EmailAddresses,PrimarySMTPAddress,ExternalEmailAddress,DisplayName,RecipientType,RecipientTypeDetails,ExchangeGuid)
                
                Out-LogFile -string "Distribution group membership recorded."
            }
        }
        else 
        {
            out-logfile -string "Using Exchange Online to obtain unified group member properties."

            if ($getUnifiedMembers -eq $TRUE)
            {
                Out-LogFile -string "Using Exchange Online to obtain the unified group membership membership."

                $functionDLMembership=@(get-O365UnifiedGroupLinks -identity $groupSMTPAddress -linkType $functionMembersLinkType -resultsize unlimited -errorAction STOP)
                
                Out-LogFile -string "Distribution group membership recorded."
            }
            else
            {
                out-logfile -string "Call is not for unified group members."
            }

            if ($getUnifiedOwners -eq $TRUE)
            {
                Out-LogFile -string "Using Exchange Online to obtain the unified group owners membership."

                $functionDLMembership=@(get-O365UnifiedGroupLinks -identity $groupSMTPAddress -linkType $functionOwnersLinkType -resultsize unlimited -errorAction STOP)
                
                Out-LogFile -string "Distribution group owners recorded."
            }
            else
            {
                out-logfile -string "Call is not for unified group owners."
            }

            if ($getUnifiedSubscribers -eq $TRUE)
            {
                Out-LogFile -string "Using Exchange Online to obtain the unified group subscribers membership."

                $functionDLMembership=@(get-O365UnifiedGroupLinks -identity $groupSMTPAddress -linkType $functionSubscribersLinkType -resultsize unlimited -errorAction STOP)
                
                Out-LogFile -string "Distribution group subscribers recorded."
            }
            else
            {
                out-logfile -string "Call is not for unified group subscribers."
            }
        }
        
        Out-LogFile -string "END GET-O365DLMEMBERSHIP"
        Out-LogFile -string "********************************************************************************"
        
        #Return the membership to the caller.
        
        return $functionDLMembership
    }