Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PowerShell - TextOnly Email (fix column output)

Posted on 2014-09-15
2
Medium Priority
?
605 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

Changing a few Outlook Options can help keep you organized!
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 …
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: …
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

722 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