How to import a text file into powershell and email it, formatted as HTML

Posted by Don on Server Fault See other posts from Server Fault or by Don
Published on 2012-04-06T20:06:24Z Indexed on 2012/04/06 23:33 UTC
Read the original article Hit count: 262

Filed under:
|

I'm trying to get a list of all Exchange accounts, format them in descending order from largest mailbox and put that data into an email in HTML format to email to myself. So far I can get the data, push it to a text file as well as create an email and send to myself. I just can't seem to get it all put together. I've been trying to use ConvertTo-Html but it just seems to return data via email like "pageFooterEntry" and "Microsoft.PowerShell.Commands.Internal.Format.AutosizeInfo" versus the actual data. I can get it to send me the right data if i don't tell it to ConvertTo-Html, just have it pipe the data to a text file and pull from it, but it's all ran together with no formatting. I don't need to save the file, i'd just like to run the command, get the data, put it in HTML and mail it to myself. Here's what I have currently:

#Connects to Database and returns information on all users, organized by Total Item Size, User
$body = Get-MailboxStatistics -database "Mailbox Database 0846468905" | where {$_.ObjectClass -eq “Mailbox”} | Sort-Object TotalItemSize -Descending | ft @{label=”User”;expression={$_.DisplayName}},@{label=”Total Size (MB)”;expression={$_.TotalItemSize.Value.ToMB()}}  -auto | ConvertTo-Html

#Pause for 5 seconds for Exchange 
write-host -foregroundcolor Green "Pausing for 5 seconds for Exchange"
Start-Sleep -s 5


$toemail = "[email protected]" # Emails report to this address.
$fromemail = "[email protected]" #Emails from this address.
$server = "Exchange.company.com" #Exchange server - SMTP.


#Email the report.
$email = New-Object System.Net.Mail.MailMessage
$email.IsBodyHtml = $True
$email.To.Add($toemail)
$email.From = $fromemail
$email.Subject = "Exchange Mailbox Sizes"
$email.Body = $body
$client = New-Object System.Net.Mail.SmtpClient $server
$client.UseDefaultCredentials = $true
$client.Send($email)

Any thoughts would be helpful, thanks!

© Server Fault or respective owner

Related posts about scripting

Related posts about powershell