Script Repository


Save mailbox size to Active Directory attribute

February 24, 2021
1923

The script saves mailbox size to an Active Directory attribute. This can be used to quickly assess mailbox size of multiple mailboxes at once, for example, if you want to include the mailbox size in reports.

To use the script in your environment, create a scheduled task that runs it on a periodical basis. The task must be configured for the User object type.

Parameter:

  • $attributeName - Specifies the name of the attribute to hold the mailbox size.
Edit Remove
PowerShell
$attributeName = "adm-CustomAttributeText1" # TODO: modify me

# Get Exchange properties
$mailboxParams = $Context.TargetObject.GetMailParameters()
$sizeInBytes = $mailboxParams.UsageInfo.Size.TotalSize

if ($sizeInBytes -eq $NULL)
{
    return
}

# Get mailbox size
switch -Regex ([Math]::Truncate([Math]::Log($sizeInBytes, 1024))) {
    '^1' { $sizeDisp = "{0:N2} KB" -f ($sizeInBytes / 1KB) }
    '^2' { $sizeDisp = "{0:N2} MB" -f ($sizeInBytes / 1MB) }
    '^3' { $sizeDisp = "{0:N2} GB" -f ($sizeInBytes / 1GB) }
    '^4' { $sizeDisp = "{0:N2} TB" -f ($sizeInBytes / 1TB) }
    Default { $sizeDisp = "$sizeInBytes Bytes" }
}

# Update attribute
$Context.TargetObject.Put($attributeName, $sizeDisp)
$Context.TargetObject.SetInfo()

The following script allows you to email an HTML-formatted report on the size of each Exchange mailbox.

To schedule the report, create a scheduled task configured for the Domain-DNS object type that runs the script and assign it over any of your AD domains. To add the script to a scheduled task, use the Run a program or PowerShell script action.

Parameters:

  • $attributeName - Specifies the name of the attribute that holds the mailbox size.
  • $to - Specifies a comma-separated list of recipients of the report.
  • $subject - Specifies the email message subject.
  • $reportHeader - Specifies the email message header.
  • $reportFooter - Specifies the email message footer.
Edit Remove
PowerShell
$attributeName = "adm-CustomAttributeText1" # TODO: modify me

# Email message settings
$to = "recipient@domain.com" # TODO: modify me
$subject = "Mailbox Size Report" # TODO: modify me
$reportHeader = "<h1><b>Sizes of All Mailboxes</b></h1><br/>"
$reportFooter = "<hr /><p><i>Please do not reply to this e-mail, it has been sent to you for notification purposes only.</i></p>" # TODO: modify me

function SearchObjects($filter, $propertiesToLoad)
{
    # Set search parameters
    $searcher = $Context.BindToObject("Adaxes://rootDSE")
    $searcher.SearchFilter = $filter
    $searcher.SearchScope = "ADS_SCOPE_SUBTREE"
    $searcher.PageSize = 500
    $searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
    $searcher.SetPropertiesToLoad($propertiesToLoad)
    $searcher.VirtualRoot = $True
    
    try
    {
        # Perform the search
        $searchResultIterator = $searcher.ExecuteSearch()
        $searchResults = $searchResultIterator.FetchAll()
        
        return ,$searchResults
    }
    finally
    {
        # Release resources
        if ($searchResultIterator){ $searchResultIterator.Dispose() }
    }
}

# Get the default Web Interface address
$webInterfaceAddress = "%adm-WebInterfaceUrl%"
if ([System.String]::IsNullOrEmpty($webInterfaceAddress))
{
    $Context.LogMessage("Default web interface address not set for Adaxes service. For details, see http://www.adaxes.com/help/?HowDoI.ManageService.RegisterWebInterface.html", "Warning")
}

# Find all users with mailboxes
$searchResults = SearchObjects "(&(sAMAccountType=805306368)(mailNickname=*)(msExchHomeServerName=*))" @($attributeName, "objectGUID")

# Add information on each mailbox to the report
$reportRecords = New-Object "System.Text.StringBuilder"

foreach ($searchResult in $searchResults)
{
    $mailboxSize = $searchResult.Properties[$attributeName].Value

    if ([System.String]::IsNullOrEmpty($mailboxSize))
    {
        continue
    }
    
    $username = $Context.GetDisplayNameFromAdsPath($searchResult.AdsPath)
    $guid = [Guid]$searchResult.Properties["objectGUID"].Value
    $reportRecords.Append("<tr><td><a href='$webInterfaceAddress`ViewObject.aspx?guid=$guid'>$username</a></td><td>$mailboxSize</td></tr>")
}

# Build report
if ($reportRecords.Length -eq 0)
{
    $html = $reportHeader + "<b>No Exchange mailboxes found</b>" + $reportFooter
}
else
{
    $html = $reportHeader + "<table><tr><th>Display Name</th><th>Mailbox Size</th></tr>" + $reportRecords.ToString() + "</table>" + $reportFooter
}

# Send mail
$Context.SendMail($to, $subject, $NULL, $html)


Comments ( 0 )
No results found.
Leave a comment