PowerShell email as body

Soldato
Joined
28 Sep 2008
Posts
14,167
Location
Britain
Hi all,

Ignoring the SharePoint element of this, all I'm really trying to do is make the result display in the body of the email. it doesn't The attachment is fine, renders fine and pulls the CSS fine.

I have to assume that it is because I'm not really closing off the HTML anywhere, but not sure.....

Anyway, here's the code, sanitised slightly, but you get the idea:

PHP:
# Add SharePoint snap-in
Add-PSSnapin Microsoft.SharePoint.PowerShell

# Set Variables
$Today = Get-Date -Format "dd-MM-yyy_hh-mm-ss"
$OutputPath = "E:\SiteCollectionSize"
$OutputFileName = "SPSCsizes-$Today"
$OutputFile = "$OutputPath\$OutputFileName.html"
$RetentionTimeDays = "14"
#SMTP Settings

$smtpsettings = @{
To = "[email protected]"
From = "[email protected]"
Subject = "SharePoint Site Collection Sizes"
SmtpServer = "my.smtp.server"
}

#HTML and CSS Var
$style = "
<style>
body { font-family:verdana; background-color: #b0c4de; }
table { font-size: 0.7em; width: 100%; border-width: 1px; border-style: solid; border-color: #000; border-collapse: collapse; }
th { border-width: 1px; padding: 2px 6px 1px 6px; border-style: solid; border-color: #000; background-color: #778899; }
td { border-width: 1px; padding: 2px 6px 1px 6px; border-style: solid; border-color: #000; }
tr:nth-child(odd) { background-color: #d3d3d3; }
tr:nth-child(even) { background-color: #fff; }
h2 { clear: both; font-size: 0.9em; }
</style
"

# PowerShell Query for Site Collection Sizes
$query = Get-SPSiteAdministration -Limit All | select Url, @{label="GB";Expression={[System.Math]::Round($_.DiskUsed/1024MB, 0)}} | Sort-Object -Descending -Property "GB" | ConvertTo-Html -Head $style -PreContent '<h2>SharePoint Site Collection by Size</h2>' | Out-File $OutputFile

#Send email message
Send-MailMessage @smtpsettings -Attachments $OutputFile -BodyAsHtml $style -Priority Normal
 
Yes, essentially, the PowerShell query grabs the table and then outputs it to HTML using the CSS style I created. This all works fine and produces a very nice HTML report, which, when viewed in IE looks lovely.

However, I'm most satisfied with least number of clicks possible, so I want the nice formatted HTML to appear in the body of the email (and as an attachment).

Ignore the > on the end of style, I just missed it off the copy when adding it to the post.
 
Solved it. I added the body parameter to the SMTP settings section and used Get-Content $OutputFile | out-string and then moved the SMTP settings below the query, but above the send-mailmessage :)

Outlook HTML / CSS formatting is not the same, but it's near enough :)
 
Cool, thanks for the above.

I've got another issue now where I want to add the date to the H2 string here:

PHP:
# PowerShell Query for Site Collection Sizes
$query = Get-SPSiteAdministration -Limit All | select Url, @{label="GB";Expression={[System.Math]::Round($_.DiskUsed/1024MB, 0)}} | Sort-Object -Descending -Property "GB" | ConvertTo-Html -Head $style -PreContent '<h2>SharePoint Site Collection by Size</h2>' | Out-File $OutputFile

As I have the data as $today earlier in the script, I was hoping I could just escape out the PowerShell as:

PHP:
ConvertTo-Html -Head $style -PreContent @'<h2>SharePoint Site Collection by Size $today</h2>'@ | Out-File $OutputFile

But that doesn't work. I get an error about passing a string before or similar.

I want the date to pick up the h2 CSS formatting and obviously be today's date (or the date the script was run).

Any ideas?
 
Back
Top Bottom