Write powershell output to email body
I'm using this cmdlet to try to send an email which the body of email is contains the powershell output.

PS C:\ps> Send-MailMessage -From postmaster@contoso.com -To suriya@sapura.contoso.com -Subject "SCR Test Result" -Body "SCR
 result + `n + $sr.summarycopystatus" -Smtpserver send.contoso.com

The email was able to sent to my mailbox but the contents look like below:

2/8/2007 3:32 PM  SERVER! Disk Space Report Disk Drive Size(GB) Free(GB)
---------- -------- -------- C:\      8.01   2.98 E:\      60.36
54.7 F:\       150  143.41  SERVER2 Disk Space Report Disk
Drive Size(GB) Free(GB) ---------- -------- -------- C:\      7.96
4.61 E:\      60.35  31.14 F:\      68.35  48.03  SERVER3
Disk Space Report Disk Drive Size(GB) Free(GB) ---------- -------- --------
C:\      7.81   4.61 E:\      60.51  46.44 F:\      136.7
111.81 G:\      136.7  128.64 H:\      136.7  66.13 I:\
136.7

Please help to array in more readable format

April 26th, 2011 8:17am

Try using:

$output = you command | out-string

Send-MailMessage -body $output

Free Windows Admin Tool Kit Click here and download it now
April 26th, 2011 8:21am

Hi, it appears that $sr.summarycopystatus is in raw format which results in the contents being shown as above.

You can format the summarycopystatus as shown below and adding the formatted text to the body:

 

$a = [string]$sr.summarycopystatus

$a = $a.replace("SERVER","`nSERVER")

$a = $a.replace("C:","`nC:")

These will insert new line at SERVER and C: text.

After that

Send-MailMessage .... ...... -body $a .....
April 26th, 2011 3:23pm

The email is:

Name           SummaryCopyStatus    CopyQueueLength     ReplayQueueLength   LastInspectedLogTime 
----           -----------------    ---------------     -----------------   -------------------- 
SG1            Healthy         0            1601          27/4/2011 4:14:46 PM 
Second Storage Group   NotConfigured      0            0                      
SG2            Healthy         0            1290          27/4/2011 4:13:40 PM 
SG3            Healthy         0            1284          27/4/2011 4:14:41 PM 
SG4            Healthy         0            1995          27/4/2011 4:14:14 PM 
SG5            Healthy         0            2392          27/4/2011 4:14:45 PM 
SG6            Healthy         0            50           27/4/2011 4:14:41 PM 
SG7            Healthy         0            3907          27/4/2011 4:14:42 PM 
SG8            Healthy         0            3936          27/4/2011 4:14:48 PM 



It seem that the output was not arrange in correct array.

 

Free Windows Admin Tool Kit Click here and download it now
April 27th, 2011 8:58am

The output seems to be different from what you have posted earlier.

Could you provide what details will the command output?

You can preview the output in powershell or powershell ISE to check

if the rows are correct. If not, you can format the string or array before

attaching to the mail body.

April 27th, 2011 2:29pm

The command is: $scr = Get-StorageGroupCopyStatus -Server Exch01 -StandbyMachine Exch02 Send-Message -From postmaster@contoso.com -To Administrator@contoso.com -Body $scr -SMTP send.contoso.com
Free Windows Admin Tool Kit Click here and download it now
April 28th, 2011 1:34am

$scr = Get-StorageGroupCopyStatus -Server Exch01 | out-string

$FromAddress = "postmaster@contoso.com"

$ToAddress = "Administrator@contoso.com"

$MessageSubject = "Server Copy Status"

$SendingServer = "send.contoso.com"

$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject,$scr

$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer

$SMTPClient.Send($SMTPMessage)

Tested and the alignment is correct. (Didnt use send-mailmessage as 2007 do not have that command.)

 

April 28th, 2011 3:32pm

This is result via webmail is  working but my outlook still the same. I not sure it is because of my laptop resolution since on my colleagies laptop appeared to get good result

 

Free Windows Admin Tool Kit Click here and download it now
May 3rd, 2011 9:20am

This is awesome, thanks Alan!
June 23rd, 2015 9:00am

Works perfectly. Thanks!
Free Windows Admin Tool Kit Click here and download it now
September 2nd, 2015 9:34am

Thank you so much.

It's completely working!

September 4th, 2015 8:02am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics