Powershell string array email via send-message

Soldato
Joined
8 Mar 2005
Posts
3,622
Location
London, UK
Hmm;
So I'm creating a string array via:
Code:
$StringArray = New-Object System.Collections.Generic.List[System.Object]
And then insert into via:
Code:
 $StringArray.Add("Process execution times. $Server. Duration $Duration minutes") 
}
This outputs as follows:
Code:
Process execution times. uk1FLNW00005. Duration 00:00:22.0870471 minutes
Process execution times. uk1FLNW00006. Duration 00:00:21.6151800 minutes
Process execution times. uk1FLNW00222. Duration 00:00:23.0497878 minutes
Process execution times. uk1FLNW00223. Duration 00:00:22.3593577 minutes
Process execution times. uk1FLNW00224. Duration 00:00:22.3891550 minutes
Process execution times. us1FLNW00037. Duration 00:00:16.9704932 minutes
Process execution times. us1FLNW00038. Duration 00:00:18.0182680 minutes
Process execution times. us1FLNW00320. Duration 00:00:17.9848608 minutes
Process execution times. us1FLNW00321. Duration 00:00:17.6563677 minutes
Process execution times. us1FLNW00323. Duration 00:00:17.7638242 minutes
Great; now I'm trying to send the array as a body of an email via:
Code:
Send-MailMessage -To $to -From "[email protected]" -Priority low -Subject "$MailSubject Execution timings." -Body $StringArray -smtpServer "smtp.com"
OK; so that fowls up; so I've tried to expand it via
Code:
$StringArrayClean | out-string
Send-MailMessage -To $to -From "[email protected]" -Priority low -Subject "$MailSubject Execution timings." -Body $StringArrayclean -smtpServer "smtp.com"
This inserts the array into the body but the output is space delimetered. I want to by line item like how it outputs to the console. So I try to create a $body via:
Code:
$body = @"
Execution Timings

Items:
$StringArrayClean


Total execution time : $TotalExecutionTime minutes

"@
and then:
Code:
Send-MailMessage -To $to -From "[email protected]" -Priority low -Subject "$MailSubject Execution timings." -Body $body -smtpServer "smtp.com"
Same output, the string array is space delimetered :(

I've tried! Any pointers would greatly appreciated!

Thanks Paul.

EDIT - So I was seemingly doing everything right?
I actually wanted to prefix the output with a timestamp and as soon as I did that, it outputted by line. eh :)
 
Last edited:
Soldato
Joined
25 Oct 2002
Posts
2,627
You should really convert your data to HTML if you want complete control over how it will be displayed in an email.

There's a guide for easily doing that with ConvertTo-HTML here.

You would just pass the result of ConvertTo-HTML to the Body of Send-MailMessage.
 
Back
Top Bottom