Forum Discussion

fstorer's avatar
fstorer
Brass Contributor
Apr 18, 2024

Get a report showing specific shared mailboxes and Display Names of their members on one line.

Dear Community, 

I have been asked to generate an excel report showing all our shared mailboxes starting with "jobs-". For each shared mailbox we want to see the members' Display Names (not the email addresses) sorted in alphabetical order, and these should stay on one line (not in a column). I was able to get the shared mailboxes and the Display Names, but not the "one line" request. Is there anyone who can help me with the code? Many thanks in advance!

 

 

$jobs = Get-EXOMailbox -RecipientTypeDetails SharedMailbox -ResultSize Unlimited -Filter { DisplayName -like 'jobs-*' }

$JobsReport = @()
foreach ($job in $jobs) {
    $Members = Get-MailboxPermission $job.identity | Where-Object { ($_.User -like '*@*') | Sort-Object User
    foreach ($Member in $Members) {
        $DisplayName = ((Get-User $Member.User).DisplayName) -join ', '
        $JobsReport += [PSCustomObject]@{
            SharedMailbox = $job.Alias
            MemberDN      = $DisplayName
        }
    }
} 

 

 

 

  • Sorry, that's what happens when I write code directly in the browser ๐Ÿ˜„

    For the sorting bit, this should do:

    $membersString = ($memberNames | sort -Unique) -join ", "
  • Move the code to the parent loop:

     

    $jobs = Get-EXOMailbox -RecipientTypeDetails SharedMailbox -ResultSize Unlimited -Filter { DisplayName -like 'jobs-*' }
    
    $JobsReport = @()
    foreach ($job in $jobs) {
        $Members = Get-MailboxPermission $job.identity | Where-Object { ($_.User -like '*@*') | Sort-Object User
        foreach ($Member in $Members) {
            $DisplayName = ((Get-User $Member.User).DisplayName) -join ', '
            }
        $JobsReport += [PSCustomObject]@{
            SharedMailbox = $job.Alias
            MemberDN      = $DisplayName
        }
    } 
    • fstorer's avatar
      fstorer
      Brass Contributor

      Thank you VasilMichev for your reply. I tried the code, but it doesn't work. For each shared mailbox I get only one name and not the full list of members. Is there something else I am missing? 

      • Ah, I forgot to append the value. Try with:

        $jobs = Get-EXOMailbox -RecipientTypeDetails SharedMailbox -ResultSize Unlimited -Filter { DisplayName -like 'jobs-*' }
        
        $JobsReport = @()
        foreach ($job in $jobs) {
            $Members = Get-MailboxPermission $job.identity | Where-Object { ($_.User -like '*@*') | Sort-Object User
            foreach ($Member in $Members) {
                $DisplayName += ((Get-User $Member.User).DisplayName)
                }
            $JobsReport += [PSCustomObject]@{
                SharedMailbox = $job.Alias
                MemberDN      = $DisplayName -join ","
            }
        } 

         

Resources