locked
Get Members.count for AD Group from Mailbox Calculated Property RRS feed

  • Question

  • Goal: Find Mailboxes with non-empty permission groups and export statistics into CSV.

    I'm working on a cleanup program for my agency's Shared Mailboxes. We use AD groups to control access with Owner, Publisher/Editor, etc levels. The SAMaccountnames and mailnicknames are a mess, so I have to piece together the properties to search with by taking the mailnickname of the mailbox and appending the information to match the permission groups' naming convention. I've got the pieces, but the puzzle won't quite come together. Here's what I have:

    $ErrorActionPreference = "Stop"
    $creds = Import-Clixml -Path ${env:USERNAME}_cred.xml
    $session = New-PSSession -Authentication basic -Credential $creds -ConnectionUri *Exchange/powershell.url* -Configuration microsoft.exchange
    Import-PSSession $session -AllowClobber
    
    
    $results = @()
    $Mailboxes = Get-mailbox -RecipientTypeDetails sharedmailbox -Resultsize unlimited -Filter "DisplayName -like 'Agency-*'" | Select PrimarySmtpAddress, DisplayName, Alias, @{n='shortname';e={$Mailbox.PrimarySmtpAddress.ToString().split("@")[0]}}
    $Stats_CSV = ".\Shared_Mailbox_Stats.csv"
    
    
    		foreach ($Mailbox in $Mailboxes){
                $short = $Mailbox.shortname
                $groupAlias = "Agency-$short-O"
                $Members =  Get-ADGroup -Filter { mailNickname -like "$groupalias" } -Properties * | Get-ADgroupmember
                if($Members.Count -ne $null){
                        $results += Get-MailboxStatistics -Identity $Mailbox.DisplayName | Select DisplayName, LastLogonTime, `
    					@{name="TotalItemSize (MB)"; expression={[math]::Round( `
    					($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}}, `
    					ItemCount
                        }
                }
            $results | Export-CSV $Stats_CSV -Force -NoTypeInformation

    The $groupalias is returning output with the correct mailnickname for the Owner Group, but Get-ADGroup is returning empty results. If I paste that $groupalias output into the $Members block, I get what I want. I'm not sure why the Calculated Object is not working through the variable.

    Thursday, July 19, 2018 3:20 PM

Answers

  •  $Members =  Get-ADGroup -Filter "mail -eq '$groupalias'" -Properties * | Get-ADgroupmember

    \_(ツ)_/



    • Edited by jrv Thursday, July 19, 2018 3:29 PM
    • Marked as answer by Justin H, Thursday, July 19, 2018 3:45 PM
    Thursday, July 19, 2018 3:27 PM