Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 

This script should be customized before running in your environment. 

All Exchange servers which hold a "Mailbox" role will be queried.

The below snippet shows the disks that data is gathered for. For our environment, the mounted LUNs are either labeled with LUN01, LUN02, etc. or have a name of LUN01, etc..

Get-WmiObject -ComputerName $server win32_volume -Filter "DriveType='3'" | Where-Object {$_.Label -like "LUN*" -OR $_.name -like "*LUN*"}

This is currently being run on a weekly basis via Task Scheduler and send email out via SMTP and saves a local HTML report.

Script can be run from the shell and output will be displayed.

<# 
.Synopsis 
   Get-ExchangeDiskspace.ps1 - Gathers LUN Diskspace from each mailbox server 
.DESCRIPTION 
   Queries for all Exchange mailbox servers - parses disks to locate LUNs and gathers stats. Exports to report and sends email. 
.EXAMPLE 
   .\Get-ExchangeDiskspace.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 
    V1.01 - 6/26/17 - Added removal of existing backup report before creating new backup. 
#> 
 
[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) 
{ 
    if (-not (Get-PSSession).ConfigurationName -eq "Microsoft.Exchange") { 
        . $env:ExchangeInstallPath\bin\RemoteExchange.ps1 
        Connect-ExchangeServer -auto -AllowClobber 
        Write-Host "Established Remote Exchange Session" 
    } else { 
        Write-Host "Exchange Session Already Established" 
    } 
} 
else 
{ 
    Write-Warning "Exchange Server management tools are not installed on this computer." 
    EXIT 
} 
 
 
#Declaring output variables 
$now = Get-Date 
$date = $now.ToShortDateString()     
$titleDate = get-date -uformat "%m-%d-%Y - %A" 
$outPath = "C:\scripts\logs\Exchange_DiskSpace.htm" 
$backupPath = "C:\scripts\logs\Exchange_DiskSpace_Backup.htm" 
$reportemailsubject = "Exchange Diskspace 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" 
    } 
 
#Backup of Previous Log file, Removal of last backup 
if (Test-Path $outPath) { 
    if (Test-Path $backupPath) { 
        Remove-Item -Path $backupPath -Force 
        } 
    Move-Item -Path $outPath -Destination $backupPath -Force  
    } 
     
#Gathering Data 
$exchangeServers = Get-ExchangeServer | Where-Object {$_.ServerRole -contains "Mailbox"} | sort name 
$LUNSize = @{} 
 
foreach ($server in $exchangeServers) { 
    $LUNSize["$($server.name)"] = @{} 
    $LUNSize.$($server.name) = Get-WmiObject -ComputerName $server win32_volume -Filter "DriveType='3'" | Where-Object {$_.Label -like "LUN*" -OR $_.name -like "*LUN*"} | foreach { 
        New-Object PSObject -Property @{ 
            LUN = $_.Label 
            FreeSpace_TB = ([MATH]::Round($_.FreeSpace / 1TB,2)) 
            TotalSize_TB = ([MATH]::Round($_.Capacity / 1TB,2)) 
            Name = $server.Name 
            PercentFree = ([MATH]::Round(($_.FreeSpace/$_.Capacity)*100,2)) 
            }  
        } 
    } 
 
#Writing output to console 
foreach ($server in $exchangeServers) { 
    $LUNSize.$($server.name) | ft -AutoSize | Out-Host 
    } 
 
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: 2px; 
                padding-bottom: 0px; 
                padding-left: 2px; 
            } 
            body { 
                margin-top: 5px; 
                margin-bottom: 10px; 
                margin-left: auto;  
                margin-right: auto; 
                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>DiskSpace Report for $titledate</strong></font> 
            </td> 
            </tr> 
            </table> 
    " 
    $htmltail = "</body> 
                </html>" 
    if ($ReportMode) { 
        $header | Out-File $outPath 
        } 
    foreach ($server in $exchangeServers) { 
     
        $htmlbody += [PSCustomObject]$LUNSize.$($server.name) | ConvertTo-Html -Fragment -Property Name,LUN,FreeSpace_TB,TotalSize_TB,PercentFree 
        if ($ReportMode) { 
            [PSCustomObject]$LUNSize.$($server.name) | ConvertTo-Html -Fragment -Property Name,LUN,FreeSpace_TB,TotalSize_TB,PercentFree | Out-File $outPath -Append  
            $htmltail | Out-File $outPath -Append 
            } 
        } 
    $htmlreport = $header + $htmlbody + $htmltail 
 
    if ($SendEmail) { 
        $htmlreport = $header + $htmlbody + $htmltail 
        Send-MailMessage @smtpsettings -Body $htmlreport -BodyAsHtml -Encoding ([System.Text.Encoding]::UTF8) 
        } 
}