Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 

This Cmdlet will gather all of the databases mounted on each Exchange server and outputs the database name, server name, database size and available mailbox space.

  • -ReportMode will output the data to an HTML file
    • Edit $outPath with the wanted file path
  • -SendEmail will send an HTML enabled email with the data
    • Edit #SMTP Settings section to reflect your environment

Script is currently being used as a weekly report being sent out via SMTP.

<# 
.Synopsis 
   Get-DatabaseSizes.ps1 - Gathers Database Sizes 
.DESCRIPTION 
   Queries for all Exchange databases within the environment and gathers sizes. Exports to report and sends email. 
.EXAMPLE 
   .\Get-DatabaseSizes.ps1 -ReportMode -SendEmail 
        Checks all servers in the organization, outputs the results to an HTML report and 
        emails the HTML report to the address configured in the script. 
.PARAMETER ReportMode 
    Set to $true to generate a HTML report. A default file name is used if none is specified. 
.PARAMETER SendEmail 
    Sends the HTML report via email using the SMTP configuration within the script. 
.NOTES 
    Written by Jeremy Corbello 
    V1.0 - 6/7/17 
#> 
 
[CmdletBinding()] 
param (   
        [Parameter( Mandatory=$false)] 
        [switch]$ReportMode, 
         
        [Parameter( Mandatory=$false)] 
        [switch]$SendEmail 
    ) 
 
#Add Exchange snapin if not already loaded in the PowerShell session 
if (Test-Path $env:ExchangeInstallPath\bin\RemoteExchange.ps1) 
{ 
    . $env:ExchangeInstallPath\bin\RemoteExchange.ps1 
    Connect-ExchangeServer -auto -AllowClobber 
    Write-Host "Established Remote Exchange Session" 
} 
else 
{ 
    Write-Warning "Exchange Server management tools are not installed on this computer." 
    EXIT 
} 
 
#Declaring output variables 
$now = Get-Date 
$date = $now.ToShortDateString()     
$outPath = "C:\scripts\logs\Exchange_Database_Sizes.htm" 
$backupPath = "C:\scripts\logs\Exchange_Database_Sizes_Backup.htm" 
$reportemailsubject = "Exchange Database Report - $date" 
 
#SMTP Settings 
$smtpsettings = @{ 
    To =  "This email address is being protected from spambots. You need JavaScript enabled to view it." 
    From = "This email address is being protected from spambots. You need JavaScript enabled to view it." 
    Subject = "$reportemailsubject" 
    SmtpServer = "smtp.jeremycorbello.com" 
    } 
$titleDate = get-date -uformat "%m-%d-%Y - %A" 
#Data Gathering 
$Databases = Get-MailboxDatabase -Status -IncludePreExchange2013 | sort name | select name,server,@{name=”Database Size (GB)”; expression={[math]::Round(($_.DatabaseSize.ToString().Split(“(“)[1].Split(” “)[0].Replace(“,”,””)/1GB),2)}},@{name=”Available Mailbox Space (GB)”; expression={[math]::Round(($_.AvailableNewMailboxSpace.ToString().Split(“(“)[1].Split(” “)[0].Replace(“,”,””)/1GB),2)}} 
 
if ($ReportMode -or $SendEmail) { 
    $header = " 
            <html> 
            <head> 
            <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'> 
            <title>DiskSpace Report</title> 
            <STYLE TYPE='text/css'> 
            <!-- 
                table { 
                        border: thin solid #666666; 
                        margin-left: auto;  
                        margin-right: auto; 
                } 
            td { 
                font-family: Tahoma; 
                font-size: 13px; 
                border-top: 1px solid #999999; 
                border-right: 1px solid #999999; 
                border-bottom: 1px solid #999999; 
                border-left: 1px solid #999999; 
                padding-top: 0px; 
                padding-right: 3px; 
                padding-bottom: 0px; 
                padding-left: 3px; 
            } 
            body { 
                margin-left: 5px; 
                margin-top: 5px; 
                margin-right: 0px; 
                margin-bottom: 10px; 
                table { 
                border: thin solid #000000; 
            } 
            --> 
            </style> 
            </head> 
            <body> 
            <table width='100%'> 
            <tr bgcolor='#CCCCCC'> 
            <td colspan='7' height='25' align='center'> 
            <font face='tahoma' color='#003399' size='4'><strong>Mailbox Database Report for $titledate</strong></font> 
            </td> 
            </tr> 
            </table> 
        " 
    $htmlbody = $Databases | ConvertTo-Html -Fragment 
    $htmltail = "</body> 
                </html>" 
} 
 
#Writing data to console 
$Databases | Out-Host 
#Backup of Previous Log file, Removal of last backup 
if ($ReportMode) { 
    if (Test-Path $outPath) { 
        Move-Item -Path $outPath -Destination $backupPath -Force 
        } 
    $header | Out-File $outPath 
    $htmlbody | Out-File $outPath -Append 
    $htmltail | Out-File $outPath -Append 
    } 
 
if ($SendEmail) { 
    $htmlreport = $header + $htmlbody + $htmltail 
    Send-MailMessage @smtpsettings -Body $htmlreport -BodyAsHtml -Encoding ([System.Text.Encoding]::UTF8) 
    }