Solved

PowerShell - TextOnly Email (fix column output)

Posted on 2014-09-15
2
511 Views
Last Modified: 2014-09-16
Experts,

I have a working script that can successfully send email to users, informing them of their excessive mailbox size and requesting them to assist in cleaning it up to make space on the back-end.  Prior to going into production with this thing, I'd like to clean up the output a little.

The script works, however, the column spacing gets misaligned within outlook (Outlook & MS Office spaces take less real estate than other characters).  Even pasting in Experts-Exchange will give me the same "ugly" output.

For Example:

Name                          FolderPath                    FolderSize                                    ItemsInFolder
----                          ----------                    ----------                                    -------------
Calendar                      /Calendar                     5555963605B                                           11828
Inbox                         /Inbox                        1290037840B                                           23475
Deleted Items                 /Deleted Items                236406455B                                              515
Sent Items                    /Sent Items                   227181035B                                             6298
Inbox                         /Sent Items/Change Control... 58623161B                                              3680
Conflicts                     /Sync Issues/Conflicts        49249238B                                               553
Inbox                         /Inbox/BTT Extracts/Inbox     47278216B                                              3503
Inbox                         /Sent Items/Change Control... 47219534B                                              3502
Drafts                        /Drafts                       39207751B                                               177

Copying and pasting this into "notepad" lines the columns up perfectly.  Would somebody know if it's possible to keep this text-only, but make the spacing a bit more readable?

Here's a copy of the scrubbed PS script.

// PowerShell script =========================================================================

$strMailboxServer = "ScrubbedOutput.none.Com"
$intSizeLimit = "7Gb"
$objSMTPFrom = New-Object System.Net.Mail.MailAddress("DoNotReply@none.com", "Exchange System Administration")
$strSMTPMessageSubject = "Action required - Your mailbox is in excess of $intSizeLimit"
$strSMTPMessageBody = "$strDisplayName, $strDynamicContent"
$vbCrLf = [Char]13 + [Char]10
$strSMTPRelayDNS = "relay.none.com"
$PSEmailServer = $strMailboxServer

$arrMailboxInfo = Get-MailboxServer $strMailboxServer |
      Get-MailboxStatistics |
      Where-Object {$_.TotalItemSize -ge $intSizeLimit}

ForEach ($objMailbox in $arrMailboxInfo) {
      $strID = $objMailbox.Identity
      $strDisplayName = $objMailbox.DisplayName
      $strPrimarySMTP = Get-Mailbox $strID | Select PrimarySmtpAddress
      $strMailboxSizeDistribution = Get-MailboxFolderStatistics $strID |
            Where-Object {$_.FolderSize -ge 25Mb} |
            Sort FolderSize -Descending |
            Select -First 10 |
            FT Name,FolderPath,@{Name="Size (Mb)";Expression={[math]::Round($_.FolderSize.ToBytes()/1MB)}},@{Expression={$_.ItemsInFolder};Label="# of Items"} -Auto | Out-String
      $strMailboxSizeDistribution = [String]::Join([Environment]::NewLine, $strMailboxSizeDistribution)
      $objSMTPTo = New-Object System.Net.Mail.MailAddress("$strPrimarySMTP", "$strDisplayName")
      $objSMTPMessage = New-Object System.Net.Mail.MailMessage ($objSMTPFrom, $objSMTPTo)
      $objSMTPMessage.Subject = "Action required - Your mailbox is in excess of $intSizeLimit"
      $objSMTPMessage.Body = `
            $strDisplayName + "," + $vbCrLf + $vbCrLf + `
            "The size of your mailbox has grown very large, and is in excess of $intSizeLimit.  Please take corrective action and remove any items that are no longer required." + $vbCrLf + $vbCrLf + `
            "The following folders are listed in order to assist in the process of identifying where items can be removed:" + $vbCrLf + `
            $strMailboxSizeDistribution + $vbCrLf + `
            "*Keep in mind that items retained in your Deleted Items folder continue to take space.  If these are not needed, please delete them." + $vbCrLf + `
            "Your cooperation is greatly appreciated.  If you require further assistance, please contact your local help desk."
      $objSMTPMessage.IsBodyHtml = $False
      $objMailer = New-Object System.Net.Mail.SmtpClient($strSMTPRelayDNS)
      $objMailer.Send($objSMTPMessage)
      }

// ========================================================================================
0
Comment
Question by:usslindstrom
2 Comments
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
Comment Utility
The issue here is the use of fixed-width vs. non-fixed-width fonts.  If you're sending in plain text the only fix is to have the client machine's email client used a fixed-width font for displaying plain text emails.  Your only other option is to send the email as HTML where you can specify what font to use or create tables so that text is aligned by the cells.
0
 
LVL 5

Author Closing Comment

by:usslindstrom
Comment Utility
Much appreciated on the assistance.  You're absolutely right.

Based on your two suggested options, I actually opted to go yet another route - and export results to a text file, then attach the file to the outbound E-mail.  Much appreciated.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
Outlook Free & Paid Tools
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now