Email Database Sizes

Matt stuck his head around cube wall today and asked me “ can you write something that will email database sizes every day” Sounded like a fun project for me. Below is what I came up with. A simple 3 line command you can paste into the command shell or turn into a PS1 file and run via a scheduler.

Get-MailboxDatabase -status | Select-Object Server,Name, @{expression={$_.AvailableNewMailboxSpace.ToGB()};label="WhiteSpace (GB)"}, @{expression={$_.DatabaseSize.ToGB()};label=”Database Size (GB)”} | Sort-Object Name | convertto-html |set-content bob.html

# line break – Above grabs the DB sizes

$Body = get-content c:\bob.html

# line break – Above grabs the HTML file and sticks it in a vrable

send-mailmessage -from "EnterpriseMessagingTeam@cohesivelogic.com" –to "EnterpriseMessagingTeam@cohesivelogic.com" –subject “Database Sizes” -smtpserver mailgateway -bodyashtml "$body"

# line break – Above sends the email – replace the things in bold with what you want

Line by line the command does the following:

  1. Grabs all of the DB names, calculates white space, works out the DB size, converts the numbers in to a readable GB format, converts the output to html, then sticks the output into a file named bob.html
  2. Put the contents of the Bob.html file into a variable we can use later in the send command
  3. The send commands sets a message from X to X with a subject of Database Sizes via the mailgateway SMTP server with an HTML formatted message body that contains the file BOB.

Below is a image example of what the email looks like

image

UPDATE – I made a change and got it down to a 2 liner

$Body = Get-MailboxDatabase -status | Select-Object Server,Name, @{expression={$_.AvailableNewMailboxSpace.ToGB()};label="WhiteSpace (GB)"}, @{expression={$_.DatabaseSize.ToGB()};label=”Database Size (GB)”} | Sort-Object Name | convertto-html

# line break – Above grabs the DB sizes and sticks into the $Body variable

send-mailmessage -from "EnterpriseMessagingTeam@cohesivelogic.com" –to "EnterpriseMessagingTeam@cohesivelogic.com" –subject “Database Sizes” -smtpserver mailgateway -bodyashtml "$body"

# line break – Above sends the email – replace the things in bold with what you want

Related Posts with Thumbnails

About Kevinm