Check DB and Log Size in Exchange 2007 (Powershell)

This Script Creates a HTML File that counts the amount of log files and DB size of each  Storage group in a Mailbox Server.

With this Script you can monitor the growth of the DB size by saving these log files and reviewing them later on.


Download The Script

 


# Author : Assaf Miron

# Http://assaf.miron.googlepages.com

# Description :

# This Script Creates a HTML File that sums the Log file count and DB size

# for each DB on the Server

# Input : Exchange Server Name

# Output: HTML File summary of the amount of Log files and DB sizes of each DB on the Server


param ( $ServerName = (Read-Host "Enter Server Name") )

# Error Checking : In case ServerName is Empty - Quit

If($ServerName -eq $null -or $ServerName -eq "") { break } 

# Add the Exchange 2007 Powershell SnapIn

Add-PSSnapin "Microsoft.Exchange.Management.Powershell.Admin"


# Set the Log file Name

$LogFile = "SizeLog.html"


#region HTML Styles and Headers

$BodyHeader = "<H1>DB Size and Log File Count</H1><H3>From Date-$((get-date).tostring('dd/MM/yyyy'))</H3>"

#Define CSS for the Log File

$CSS = "<style type=`"text/css`">

TABLE{

FONT-WEIGHT: normal;

direction:LTR;

border-collapse:collapse;

border-style: solid;

border-width: 1px;

border-Color: Black;

font-family: Calibri;

font-size: 14px;

}

td{

direction: LTR;

border-style: solid;

border-width: 1px;

border-Color: Black;

font-family: Calibri;

}

th{

border-style: solid;

border-width: 1px;

border-color: Black;

}

Body

{

PADDING-BOTTOM: 30px;

PADDING-TOP: 5px;

FONT-WEIGHT: bold;

font-family: Calibri;

font-size: 16px;

}

</style>"

$Title = $CSS+"<Title>DB Size and Log File Count</Title>"

#endregion


Function CountLogs($LogFolderName)

# Function Counts the number of log files in a folder

# Input : Log Folder Path

# Output : Count of Log Files

{

$LogFol = Dir $LogFolderName"\*.log"

$LogFileCount = $LogFol.Count

Return $LogFileCount

}


Function EDBSize($EDBFile)

# Function returns an object with the DB Name,Size

# Input : DB Path

# Output : Object of DB Name and Size

{

$DBSize = Get-Item $EdbFile | Select Name,@{Expression={($_.Length/1GB).tostring("#,###.# GB")};Name="EDB Size(GB)"}

Return $DBSize

}


Function Join-Data

# Function Joins arrays and Strings to a Single Object with Members

# I Used the same principle of the Out-DataTable and converted it to Join Objects into one

# Using the Add-Member cmdlet. the Function writes to a predefiend object named $DataObject

{

param($objName="") # This parameter is used for objects that don't have member other than Length like Strings

foreach ($item in $Input)

{

$Item.PsObject.get_properties() | foreach{

if ($_.value -eq $null)

{

$DataObject | Add-Member noteproperty $_.Name "[empty]"

}

elseif ($_.IsArray) {

$DataObject | Add-Member noteproperty $_.Name [string]::Join($_.value, ";")

}

elseif ($objName -ne "") {

$DataObject | Add-Member noteproperty $objName $Item

}

else

{

$DataObject | Add-Member noteproperty $_.Name $_.value -Force

}

}

}

return @(,$DataObject)

}


#region Check Cluster Owner

$Clu = New-Object -com "MSCluster.Cluster"

$Clu.Open($ServerName)

$GroupOwner = $Clu.ResourceGroups.Item($ServerName).OwnerNode.Name

$ClusterNodes = $Clu.Nodes | select Name

#endregion


# Create The Log File with the Styles and Headers

ConvertTo-Html -body $BodyHeader -Head $Title | Out-File $LogFile


#region Collect the Data and Create the Tables

# Collect all Mailbox Database Name from the Server

$MDB = Get-MailboxDatabase -Server $ServerName


ForEach($Node in $ClusterNodes)

{

# Check if the Node is the Active Node (Group Owner)

If($Node.Name -eq $GroupOwner) 

{

# Set the Table Header

$TableHeader = "<h2>"+$Node.Name+"(Current Cluster Owner)</h2>"

}

Else

{

# Set the Table Header

$TableHeader = "<h2>"+$Node.Name+"</h2>"

}

# Create an Empty Array for all the Servers DB Data

$AllSrvDB = @()

# Collect the Data from all Storage Groups in a Server

ForEach($DB In $MDB){

# Get the LogFolderPath from Each DB

$LogFolder = Get-StorageGroup -Identity $DB.StorageGroup | select LogFolderPath

# Replace the EDB File Path to a UNC Path

$SrvDB = $DB.EdbFilePath -replace(":","$")

$UNCEDB = "\\"+$Node.Name+"\"+$SrvDB 

# Replace the Log Folder Path to a UNC Path

$LogFolder = $LogFolder.LogFolderPath -replace(":","$")

$UNCLogFolder = "\\"+$Node.Name+"\"+$LogFolder

# Create an Empty psObject, $DataObjcet - Used by this Name in the Join-Data Function

$DataObject = New-Object psobject

# Create the Storage Group Table Object

$DB.StorageGroup.Name | Join-Data -objName "Storage Group Name" | Out-Null # String with no Values - Define a Name

EDBSize($UNCEDB) | Join-Data | Out-Null # Contians Multiple Values, No need to Define a Name

(CountLogs($UNCLogFolder)).ToString() | Join-Data -objName "Log File Count" | Out-Null # String with no Values - Define a Name

# Join the Storage Group Data to the All Server DB Data

$AllSrvDB += $DataObject

}

# Export the Object Table of the Current Server

$AllSrvDB | ConvertTo-Html -Body $TableHeader | Out-File -Append $LogFile

}

#endregion


# Open the Log File

Invoke-Item $LogFile 

Comments