Apr 14 2022 06:29 AM
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?
Apr 14 2022 06:40 AM
Apr 14 2022 07:39 AM
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:
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