mattnicola
mattnicola

Reputation: 403

PowerShell Emailing Arrays

I'm having issues with the way an array loses its formatting when it is emailed and viewed in outlook 2013

The formatted array looks like this in PowerShell

vServer                                          State             Connection                                                          
-------                                          -----             ----------                                                          
vServer-LB-1                                     UP                0                                                                   
vServer-LB-2                                     DOWN              0                                                                   
vServer-LB-3                                     UP                0                                                                   
vServer-LB-4                                     UP                0                                                                   
vServer-LB-5                                     UP                0                                                                   
vServer-LB-6                                     UP                2                                                                               

This is how I formatted the array (I have tried to email the unformatted array, but it is still wrong)

$formatserver =  @{Expression={$_.name};Label="vServer";width=48}, `
                 @{Expression={$_.state};Label="State";width=17}, `
                 @{Expression={$_.establishedconn};Label="Connection"}

$Array = $server | Format-Table $formatserver

However, when emailed (not quite like this, but its not formatted correctly).

vServer State Connection
------- ----- ----------
vServer-LB-1 UP 0
vServer-LB-2 DOWN 0
vServer-LB-3 UP 0
vServer-LB-4 UP 0
vServer-LB-5 UP 0
vServer-LB-6 UP 2

Here is the code for emailing

$from = 'Reporting <[email protected]>'
$to = '[email protected]'
$subject = 'Report'
$body = $Array | Out-String
$smtpServer = 'mail.me.com'

$msg = New-Object Net.Mail.MailMessage($from, $to, $subject, $body)
#$msg.IsBodyHTML = $true
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($msg)

Please note I have tried many combinations of | out-stringand $msg.IsBodyHTML = $true

Upvotes: 3

Views: 136

Answers (1)

Richard
Richard

Reputation: 7000

You can use the <pre> tag in HTML to keep the spacing in your emails.

$body = $Array | Out-String | %{"<pre>"+$_+"</pre>"}

Make sure that you set IsBodyHTML as $true.

Note: Table formats a limited to the buffers of your PowerShell Shell/Console. So if your table width is more that the buffer width on your Shell/Console the table will not be shown in full.

To get round this you can set your shell buffer at the start of your script with the following:

$pshost = get-host
$pswindow = $pshost.ui.rawui
$newsize = $pswindow.buffersize
$newsize.height = 3000
$newsize.width = 1500
$pswindow.buffersize = $newsize

Or go to File >> Properties on your Shell/Console, and change the following property.

enter image description here

Upvotes: 1

Related Questions