我正在尝试获取所有 Exchange 帐户的列表,按从最大邮箱开始的降序排列,并将这些数据放入 HTML 格式的电子邮件中,以便通过电子邮件发送给我自己。到目前为止,我可以获取数据,将其推送到文本文件,以及创建电子邮件并发送给自己。我似乎无法将它们全部整合在一起。我一直在尝试使用 ConvertTo-Html,但它似乎只是通过电子邮件返回数据,如“pageFooterEntry”和“Microsoft.PowerShell.Commands.Internal.Format.AutosizeInfo”,而不是实际数据。如果我不告诉它 ConvertTo-Html,我可以让它向我发送正确的数据,只需让它将数据传送到文本文件并从中提取,但所有这些都一起运行而没有格式化。我不需要保存文件,我只想运行命令,获取数据,将其放入 HTML 并邮寄给我自己。这是我目前拥有的:
#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)
任何想法都会有帮助,谢谢!
答案1
你可以试试我的方法,它对我有用。你需要设置所有变量,它们都应该是自文档化的。
$body = "$(cat $file)"
send-MailMessage -SmtpServer $smtpserver -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Priority high
答案2
感谢您的帮助。您建议的部分方法对我来说很有效,但我遇到了一些其他格式问题,最终我也解决了。为了帮助下一个人,我也发布了我的最终结果/配置。它可能没有达到应有的效率,但似乎可以满足我的要求!再次感谢!
#Connects to Database and returns information on all users, organized by DisplayName, ItemCount, TotalItemSize
$body = Get-MailboxStatistics -database "Mailbox Database 0846468905" | where {$_.ObjectClass -eq “Mailbox”} | Sort-Object TotalItemSize -Descending | ConvertTo-Html -property DisplayName, ItemCount, TotalItemSize
#Pause for 3 seconds for Exchange to write the file.
Start-Sleep -s 3
$toemail = "[email protected]" #Emails report to this address.
$fromemail = "[email protected]" #Emails report from this address.
$server = "Exchange.company.com" #Exchange server - SMTP.
$HTMLmessage = @"
<font color=""black"" face=""Arial, Verdana"" size=""3"">
<br>
<body BGCOLOR=""white"">
$body
</body>
"@
#Email the report.
Send-MailMessage -smtpServer $server -to $toemail -from $fromemail -subject "Exchange Mailbox Sizes" -body $HTMLmessage -BodyAsHtml -priority High