Pretty much every shared mailbox we have in our environment has a security group assigned to it that is the same name except starting with SG_. I'm looking to have a script that can parse through all the shared mailboxes, review the security group assigned to it and export the members out with it. I have something below but it's not complete and I'm unsure how to set it. Any suggestions are appreciated.
Ideally it would output something like:
Mailbox Name Security Group Name Access
Mailbox1 SG_Mailbox1 domain\user1
mailbox1 sg_mailbox1 domain\user2
MailboxABC SG_MailboxABC domain\userA
$sharedmailbox = Get-Mailbox -RecipientTypeDetails "shared" -ResultSize unlimited foreach ($mb in $sharedmailbox) { $securitygroup = Get-Mailbox -id $mb | Get-MailboxPermission | where { ($_.User -like "domain\SG_*") } $securitygroup = ($securitygroup.identity -split "/")[-1] #Write-Host "security group" $securitygroup.User foreach ($member in $securitygroup.User) { write-host $member $SGMember = Get-DistributionGroupMember -Identity $member Write-Host $SGMember $outarray += New-Object PsObject -property @{ 'Mailbox' = $mb 'security group' = $securitygroup.User 'SG Member' = $SGMember } } } $outarray