Get-ExchangeGroupMembers.psm1
function Get-ExchangeGroupMembers { [cmdletbinding()] param( [parameter( Mandatory = $true, Position = 0, ValueFromPipeline = $true)] [string[]]$Identity ) BEGIN { if (((Get-ConnectionInformation).State | Select-Object -Last 1) -ne "Connected") { try { Connect-ExchangeOnline -ErrorAction Stop } catch { Write-Error "Could not connect to Exchange Online. Try connecting manually then run again." return } } try { $ADUsers = Get-ADUser -Filter * -Properties title, department -ErrorAction Stop -Verbose } catch { Write-Error "Could not get AD users, check domain connectivity." return } } PROCESS { foreach ($id in $Identity) { try { $group = Get-UnifiedGroup $id -ErrorAction Stop $members = Get-UnifiedGroupLinks $group -LinkType members | Select-Object -ExpandProperty primarysmtpaddress } catch { try { $group = Get-DistributionGroup $id -ErrorAction Stop $members = Get-DistributionGroupMember $group | Select-Object -ExpandProperty primarysmtpaddress } catch { Write-Error "Could not find group information for identity $id" } } foreach ($user in $members) { $obj = [PSCustomObject]@{ GroupType = $group.RecipientTypeDetails DisplayName = $group.DisplayName PrimarySmtpAddress = $group.PrimarySmtpAddress } $results = $ADUsers | Where-Object UserPrincipalName -eq $user $results.PSObject.Properties | ForEach-Object { $obj | Add-Member -MemberType NoteProperty -Name $_.Name -Value $_.Value } $obj } } } END { <# #> } } |