如何将文本文件导入到PowerShell中,并将其格式化为HTML

我正在尝试获取所有Exchange帐户的列表,将它们按最大邮箱的降序格式排列,并将这些数据放入HTML格式的电子邮件中发送给我自己。 到目前为止,我可以获取数据,将其推送到一个文本文件以及创build一个电子邮件并发送给我自己。 我似乎无法把这一切放在一起。 我一直在尝试使用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) 

任何想法将是有益的,谢谢!

你可以试试我的。 你需要设置所有的variables,这些variables都应该是自动logging的。

 $body = "$(cat $file)" send-MailMessage -SmtpServer $smtpserver -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Priority high 

感谢您的帮助。 你的build议对我来说工作得很好,但是我还有一些其他的格式问题,我最终也搞不清楚了。 只是为了帮助下一个人,我也张贴我的最终结果/configuration。 它可能不如它的效率,但它似乎为我想要的工作! 再次感谢!

  #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