2013-10-18 37 views
0

我需要帮助将一个哈希表输出到电子邮件的正文中。在Powershell中转换Hashtable

$computer = "adwte998" 
$err = "This is the error" 
$td = Get-Date 

$table = @{ 

Workstation = $computer 
Error = $err 
Time = $td 

} 

send-mailmessage -to "[email protected]" -from "Automated Reboot<[email protected]>" -subject "E-Mail HashTable Test" -body ($table | Out-String) -smtpserver smtpserver.email.com 

消息体看起来像它应该,如果我只是返回$表

我会非常喜欢表转换为看起来像一个CSV用正确的列标题,但我不格式不想通过电子邮件将其作为附件发送。

有谁知道我可以怎么做呢?我甚至会考虑将其转换为HTML,以便在电子邮件中看起来不错。

回答

1

使用V3:

$computer = "adwte998" 
$err = "This is the error" 
$td = Get-Date 

$table = [ordered]@{ 
Workstation = $computer 
Error = $err 
Time = $td 
} 

[PSCustomObject]$table | ft -auto | out-string 

Workstation Error    Time     
----------- -----    ----     
adwte998 This is the error 10/18/2013 1:26:08 PM 

为HTML:

[PSCustomObject]$table | convertto-html 
+0

好吧,这解决了如果我要输出到屏幕上,但我想现在将其转换为HTML表的格式所以我可以在Send-MailMessage中使用-BodyAsHtml。我会怎么做? – Patrick

+0

添加了转换为html的示例 – mjolinor