Public/Get-DistributionGroupWithMember.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
function Get-DistributionGroupWithMember {
    [CmdletBinding()]
    param (
        # Group Object from Get-DistribuionGroup
        [Parameter(Mandatory=$true,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [Alias("Object")]
        $GroupObject
    )
    
    begin {
        if (-not (Get-PSSession | Where-Object { $_.ConfigurationName -eq 'Microsoft.Exchange' -and $_.ComputerName -eq 'outlook.office365.com' } )) {
            Write-Warning "Please connect to Exchange Online to convert distribution groups from AD synchronized to cloud-only; exiting script"
            Exit
        }
    }
    
    process {
        foreach($group in $GroupObject) {
            $member = Get-DistributionGroupMember -Identity $group.ExternalDirectoryObjectId
            [pscustomobject]@{
                Group = $group
                Member = $member
            }
        }
    }
    
    end {
        
    }
}