none
How can I align columns when putting results to email

    Question

  • I would appreciate it if anyone could help me. I found a script and made just a little mod to it. The script just display disk information. Output to the screen is perfect but when I try to email the report the columns are not aligned. The result in the email look like what is displayed below.

    gwmi win32_logicaldisk -Computer $Servers | where{$_.MediaType -eq 12}| Format-Table SystemName
    , DeviceId, VolumeName, MediaType, @{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.Free
    Space/1GB,2)}


    • Edited by Email Tech Wednesday, April 19, 2017 3:04 PM
    Wednesday, April 19, 2017 2:58 PM

Answers

  • How to use Format-Table in an email to get aligned columns:

    $body = '<pre>'
    $body += Get-PsDrive | Format-Table | Out-String
    $body += '</pre>'
    
    $mailprops = @{
    	To = 'to mail'
    	From = 'from mail'
    	Subject = 'My subject'
    	BodyAsHtml = $true
    	SMTPServer = 'my server'
    	Port = 25
    }
    Send-MailMessage @mailprops

    This method is seldom seen but is the easiest way to force a text block to remain formatted in HTML.


    \_(ツ)_/

    • Marked as answer by Email Tech Wednesday, April 19, 2017 4:11 PM
    Wednesday, April 19, 2017 3:34 PM

All replies

  • Email results didn't paste correctly, also please provide the relevant bits of the script.
    Wednesday, April 19, 2017 3:03 PM
  • Email is controlled by the client.  You can send HTML mail and set the font.

    Note that we do not fix scripts found on the Internet.  You must have a script that you are writing and a specific question.


    \_(ツ)_/

    Wednesday, April 19, 2017 3:04 PM
  • | Format-Table is used to only display data in the host; it won't let you export any data beyond the host.

    Remove format-table; try storing all the data in a variable, then playing around converting it to csv or directly into html; there are relevant cmdlets for both conversions if you do not need to export and store the data in a .csv file for future use.

    When sending email, be sure to convert the data into html and send the mail message with body as html.

    If you need more specific help, please provide more specific questions.

    Wednesday, April 19, 2017 3:10 PM
  • How to use Format-Table in an email to get aligned columns:

    $body = '<pre>'
    $body += Get-PsDrive | Format-Table | Out-String
    $body += '</pre>'
    
    $mailprops = @{
    	To = 'to mail'
    	From = 'from mail'
    	Subject = 'My subject'
    	BodyAsHtml = $true
    	SMTPServer = 'my server'
    	Port = 25
    }
    Send-MailMessage @mailprops

    This method is seldom seen but is the easiest way to force a text block to remain formatted in HTML.


    \_(ツ)_/

    • Marked as answer by Email Tech Wednesday, April 19, 2017 4:11 PM
    Wednesday, April 19, 2017 3:34 PM