Forum Discussion
Buutenlander
Apr 14, 2022Copper Contributor
output in mail is a single line
Hi all,
I would like to pipe output form any *GET* command to mail as body.
That works alright, but instead of getting a nicely formatted table of records i get a single line of text.
${*Username*} = Read-Host -Prompt 'Type username'
${*Result*} = Get-ADPrincipalGroupMembership ${*Username*} |select name |Out-String
Send-MailMessage -From Email address removed -Subject memberships -To Email address removed -Body ${*Result*} -BodyAsHtml -SmtpServer smtp.mydomain.com
just using the command part from line 2 gives a perfect result on terminal. So it strips the newline attribute while converting to mail I guess...
Any clues?
- LainRobertsonSilver Contributor
First, an observation.
Using -BodyAsHtml is a mistake in your script's current form, as the body's not HTML at all but simply a plain-text string.
This leads to having two options you can pursue:
- Add tags around your existing string (the $Result variable), or;
- Leave the -BodyAsHtml out.
For Option 1, you can introduce the tags you need using ConvertTo-Html instead of Out-String:
$Username = Read-Host -Prompt 'Type username' $Result = Get-ADPrincipalGroupMembership $Username | select Name | ConvertTo-Html Send-MailMessage -From Email address removed -Subject memberships -To Email address removed -Body $Result -BodyAsHtml -SmtpServer smtp.mydomain.com
Option 2 doesn't really need further explanation, though you may or may not need to fiddle with the -Encoding parameter depending on your environment.
Cheers,
Lain
- BuutenlanderCopper Contributoradd: I can also export the output to csv and add the file as attachment.
Tried that and it works, but I don't want to use attachments in this case.