Solved

PowerShell - TextOnly Email (fix column output)

Posted on 2014-09-15
2
538 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 40

Accepted Solution

by:
footech earned 500 total points
ID: 40323465
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
ID: 40325428
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
Many people use more than one email account and so it becomes difficult for them to manage them when they use separate accounts,  so, in this article, I have shared an easy way to add Other Mail Accounts in your Google Inbox. It helps to combine all…
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 …
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

839 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