locked
Formatting emailreports using Powershell RRS feed

  • Question

  • Does anyone know a good method to align text sent as email from Powershell using the -f parameter or other methods? My email reports always look something like this because I haven't found any other ways to align the text into columns

    I use (Out-String -InputObject ("{0,-100} {1,-10} {2,-20}" -f $Volume.VolumeName, ($Volume.VolumeSizeMB/1024).ToString("#"), $Volume.StoragePoolName)) to create the columns and fill in with the data

    I would like the three columns to be perfectly aligned

    Volume                                                                                               Size GB    Pool
    vcloud01                                                                                             2048       r01a-sys
    vcloud02                                                                                             2048       r01a-sys
    TestRestore-ADMFLA                                                                                   2048       u02c-archive
    TestRestore-ADMFLA2                                                                                  2048       u02c-archive
    snap-u02c-appsc1-dat-dnn-02                                                                          4096       u02c-archive
    testvol04                                                                                            1024       u02c-archive
    RIV-DO-NOT-DELETE-U04C-VSWCLU1-SYS-CNN-03                                                            2048       u04c-labman
    RIV-DO-NOT-DELETE-U04C-VSWCLU1-SYS-CNN-01                                                            2048       u04c-labman
    RIV-DO-NOT-DELETE-U04C-VSWCLU1-SYS-CNN-02                                                            2048       u04c-labman
    RIV-DO-NOT-DELETE-U04C-VSWCLU1-DAT-CNN-01                                                            2048       u04c-labman


    Freddy


    Wednesday, July 26, 2017 10:34 AM

All replies

  • Have you considered converting to html?

    $yourData = $yourData | ConvertTo-HTML

    then in your send-mailmessage command, just add the -BodyAsHtml tag.

    Send-MailMessage @smtpsettings -Body $htmlreport -BodyAsHtml -Encoding ([System.Text.Encoding]::UTF8)

    Getting a little more advanced, you could use style tags to adjust the HTML output. See an example below:

    $header = "
    		            <html>
    		            <head>
    		            <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
    		            <title>Started Notification</title>
    		            <STYLE TYPE='text/css'>
    		            <!--
            	            table {
                		            border: thin solid #666666;
                                    margin-left: auto; 
                                    margin-right: auto;
            	            }
    		            td {
    			            font-family: Tahoma;
    			            font-size: 13px;
    			            border-top: 1px solid #999999;
    			            border-right: 1px solid #999999;
    			            border-bottom: 1px solid #999999;
    			            border-left: 1px solid #999999;
    			            padding-top: 0px;
    			            padding-right: 2px;
    			            padding-bottom: 0px;
    			            padding-left: 2px;
    		            }
    		            body {
    			            margin-top: 5px;
    			            margin-bottom: 10px;
                            margin-left: auto; 
                            margin-right: auto;
    			            table {
    			            border: thin solid #000000;
    		            }
    		            -->
    		            </style>
    		            </head>
    		            <body>
    		            <table width='100%'>
    		            <tr bgcolor='#CCCCCC'>
    		            <td colspan='7' height='25' align='center'>
    		            <font face='tahoma' color='#003399' size='4'><strong> $reportemailsubject</strong></font>
    		            </td>
    		            </tr>
                        <tr>
                        <td colspan='7' height='25' align='center'>
                        <font face='tahoma' color='#003399' size='3'>This is a test email</font>
                        </td>
                        </tr>
    		            </table>
                "
            $htmlbody = Get-Mailbox | ConvertTo-Html
            $htmltail = "</body>
    			        </html>"
            $htmlreport = $header + $htmlbody + $htmltail

    Wednesday, July 26, 2017 12:46 PM
  • The easiest way to display text aligned is to use "pre" tags and a fixed font.

    $body = '<pre>' + $text" '</pre>'

    "pre" uses a fixed font and maintains line breaks. https://www.w3schools.com/tags/tag_pre.asp

    The style can be added for color/bold or other flavors.


    \_(ツ)_/


    Wednesday, July 26, 2017 3:23 PM
  • Hi,
    Just checking in to see if the information provided was helpful. Please let us know if you would like further assistance.

    Best Regards,

    Frank

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, August 11, 2017 4:32 AM
  • I'll try both suggestions and post an update


    Freddy

    Wednesday, August 30, 2017 12:17 PM
  • Have you tried using this Powershell Tip of the week and changing the formatting to match the Volume information?

    https://technet.microsoft.com/en-us/library/ee692794.aspx

    Might look something like this:

    $a = @{Expression={$Volume.VolumeName};Label="Volume";width=50}, `
         @{Expression={($Volume.VolumeSizeMB/1024).ToString("#")};Label="Size GB";width=4}, `
         @{Expression={$Volume.StoragePoolName};Label="Pool";width=15}

    Then when you run your output you can pipe it into a Format-table $a


    • Edited by subsum44 Wednesday, August 30, 2017 6:00 PM forgot to remove unwanted text
    Wednesday, August 30, 2017 6:00 PM