Answered by:
Message.Body is only returning '='

Question
-
If I run $Query from below I get the results I want. However, running the whole script returns just a '=' sign in the e-mail. Am I losing the result-set because I am converting to HTML? any advice greatly appreciated. I just want the Get-PublicFolderStatistics to be put in the $Style HTML table I have specified. Any advice greatly appreciated, thanks.
$style = "<style>BODY{font-family: Arial; font-size: 10pt;}" $style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}" $style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }" $style = $style + "TD{border: 1px solid black; padding: 5px; }" $style = $style + "</style>" $From = 'ExampleFrom@Exampledomain' $To = 'ExampleTo@Exampledomain' $Subject = 'ExampleSubject' $Query = Get-PublicFolder '\ExamplePublicFolderPath' -Recurse | Get-PublicFolderStatistics | ConvertTo-Html -Head = $Style $Message = New-Object System.Net.Mail.MailMessage $From, $To $Message.Subject = $Subject $Message.IsBodyHTML = 'True' $Message.Body = $Query $SMTPServer = 'ExampleSMTPServer' $Smtp = New-Object Net.Mail.SmtpClient($SMTPServer) $Smtp.Send($Message)
- Edited by Rhys Pickett Tuesday, January 22, 2019 10:18 AM
Tuesday, January 22, 2019 10:17 AM
Answers
-
The simple problem is this:
ConvertTo-Html -Head =$Style
You cannot use an equal sign in a parameter. Just do this:
ConvertTo-Html -Head $Style
Take some time to review basic PowerShell command usage.\_(ツ)_/
- Marked as answer by Rhys Pickett Tuesday, January 22, 2019 10:59 AM
Tuesday, January 22, 2019 10:53 AM -
This is the correct way to write this in PowerShell.
$style = @' <style>BODY{font-family: Arial; font-size: 10pt;} TABLE{border: 1px solid black; border-collapse: collapse;} TH{border: 1px solid black; background: #dddddd; padding: 5px; } TD{border: 1px solid black; padding: 5px; } </style> '@ $mailsplat =@{ From = 'ExampleFrom@Exampledomain' To = 'ExampleTo@Exampledomain' Subject = 'ExampleSubject' BodyAsHtml = $true
SmtpServer = $SMTPServer
} $body = Get-PublicFolder '\ExamplePublicFolderPath' -Recurse | Get-PublicFolderStatistics | ConvertTo-Html -Head $Style |
Out-String Send-MailMessage @mailsplat -Body $body
\_(ツ)_/
- Edited by jrv Tuesday, January 22, 2019 11:03 AM
- Proposed as answer by jrv Tuesday, January 22, 2019 11:04 AM
- Marked as answer by Rhys Pickett Tuesday, January 22, 2019 11:05 AM
Tuesday, January 22, 2019 10:57 AM
All replies
-
What if you the get-publicfolder gives only that?
do $Query and display the output to confirm.
You might want to check Get-publicfolder cmdlets
VJ
Tuesday, January 22, 2019 10:21 AM -
Hi VJ,
thanks for your reply, the $query without the ConvertTo-HTML -Head $Style shows the below.
Name ItemCount LastAccessTime ---- --------- -------------- xxxx 953 21/01/2019 08:35:37 xxx 277 21/01/2019 08:35:37
The query with ConvertTo-HTML -Head $Style on the end shows the below. It appears as though I'm being e-mailed the '=' from below but not sure why
<head> = </head><body><table> <colgroup> </col>
</col>
</col> then lots of my folder names and IDs
- Edited by Rhys Pickett Tuesday, January 22, 2019 10:38 AM
Tuesday, January 22, 2019 10:23 AM -
Oh yea. You might want to write this to a .csv file and attach to the email.
Export-pubilcfolderstatiscts script from the below link could give you that result.
https://www.microsoft.com/en-us/download/details.aspx?id=38407
You can then add your module with your script to attach that file to an email and forard.
VJ
Tuesday, January 22, 2019 10:37 AM -
The simple problem is this:
ConvertTo-Html -Head =$Style
You cannot use an equal sign in a parameter. Just do this:
ConvertTo-Html -Head $Style
Take some time to review basic PowerShell command usage.\_(ツ)_/
- Marked as answer by Rhys Pickett Tuesday, January 22, 2019 10:59 AM
Tuesday, January 22, 2019 10:53 AM -
This is the correct way to write this in PowerShell.
$style = @' <style>BODY{font-family: Arial; font-size: 10pt;} TABLE{border: 1px solid black; border-collapse: collapse;} TH{border: 1px solid black; background: #dddddd; padding: 5px; } TD{border: 1px solid black; padding: 5px; } </style> '@ $mailsplat =@{ From = 'ExampleFrom@Exampledomain' To = 'ExampleTo@Exampledomain' Subject = 'ExampleSubject' BodyAsHtml = $true
SmtpServer = $SMTPServer
} $body = Get-PublicFolder '\ExamplePublicFolderPath' -Recurse | Get-PublicFolderStatistics | ConvertTo-Html -Head $Style |
Out-String Send-MailMessage @mailsplat -Body $body
\_(ツ)_/
- Edited by jrv Tuesday, January 22, 2019 11:03 AM
- Proposed as answer by jrv Tuesday, January 22, 2019 11:04 AM
- Marked as answer by Rhys Pickett Tuesday, January 22, 2019 11:05 AM
Tuesday, January 22, 2019 10:57 AM -
Thanks Vj, I'm sure this would work also I will opt for this method after attempting the amendment below from JRVTuesday, January 22, 2019 10:58 AM
-
Thanks for this JRV, completely overlooked that. Should have been straight forward as you said with the results changing when I added that bit on...
Tuesday, January 22, 2019 10:59 AM -
This is the best way to build a style block.
$style = @' <style> body{ font-family: Arial; font-size: 10pt; } table{ border: 1px solid black; border-collapse: collapse; } th{ border: 1px solid black; background: #dddddd; padding: 5px; } td{ border: 1px solid black; padding: 5px; } </style> '@
\_(ツ)_/
Tuesday, January 22, 2019 11:02 AM -
This looks much neater in arrays thank you, and thanks for you time on this.Tuesday, January 22, 2019 11:09 AM
-
Tuesday, January 22, 2019 11:12 AM