Get-EXOMailbox and expand AzureADGroupMember

Viewed 39

I want to obtain a list of all shared mailboxes and expand the group membership.

I have the following

Get-EXOMailbox -RecipientTypeDetails SharedMailbox -ResultSize:Unlimited | Get-MailboxPermission | select *user  | where { ($_.User -Notlike 'NT*' -and $_.User -Notlike '*@*')   }

This returns all the group memberships. How do I pipe this into the following please?

Get-AzureADGroup -SearchString <variable> | Get-AzureADGroupMember | Select DisplayName, Mail

I have tried this:

$groupnames = (Get-EXOMailbox -RecipientTypeDetails SharedMailbox -ResultSize:Unlimited | Get-MailboxPermission | select *user  | where { ($_.User -Notlike 'NT*' -and $_.User -Notlike '*@*')   })
    
foreach($item in $groupnames){
   $item
   Get-AzureADGroup -SearchString $item | Get-AzureADGroupMember | Select DisplayName, Mail
}
1 Answers

If the first command gives you a list of groups you need to store this information in a variable:

$groups = Get-EXOMailbox -RecipientTypeDetails SharedMailbox -ResultSize:Unlimited | Get-MailboxPermission | select *user  | where { ($_.User -Notlike 'NT*' -and $_.User -Notlike '*@*')   }

I assume the array $groups looks like this:

$groups = @(
    "ACL_Awards Mailbox Access"
    "ACL_Business Analyst Mailbox Access" 
    "ACL_Callsafe Operatives" 
    "ACL_Certs Mailbox"
    "ACL_Check Calls Mailbox Access"
)

Then the following should work.

$result = @(   
    #Build filter
    $filterArray = @(
        $groups | %{
            "DisplayName eq '$_'"
        }
    )
    $filterString = $filterArray -join " or "
    #Query Groups
    $groups = get-AzureAdGroup -filter $filterString 
    foreach ($group in $groups){
        #Query members
        $members = get-AzureAdGroupMember -all -objectId $group.objectId
        #Build objects to return
        $attrsht = @{
            group=$group.DisplayName
            members=$members
        }
        New-Object -typename psobject -Property $attrsht
    }
)
Related