2013-10-18 39 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