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!
you can try mine which works for me. You need to set all the variables, which should all self-documenting.
Thanks for your help above. What you suggested worked fine for me in part, but I had some other formatting issues that I ended up figuring out as well. Just to help the next person looking I'm posting my end result/configuration as well. It may not be as efficient as it could be but it seems to work for what I wanted! Thanks again!