Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PowerShell - TextOnly Email (fix column output)

Posted on 2014-09-15
2
Medium Priority
?
623 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 41

Accepted Solution

by:
footech earned 2000 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

WatchGuard Case Study: Museum of Flight

“With limited money and limited staffing, we didn’t have a lot of choices in terms of what we could do to bring efficiency. WatchGuard played a central part in changing that.” To provide strong, secure Wi-Fi access within the museum, Hunter chose to deploy WatchGuard’s AP120 APs.

Question has a verified solution.

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

Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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: …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

885 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