在Powershell中转换Hashtable

时间:2013-10-18 17:16:39

标签: powershell hashtable

我需要一些帮助输出哈希表到电子邮件正文。

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

$table = @{

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

} 

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

如果我刚刚返回$ table

,Message body看起来应该是这样的

我希望将表格转换为具有正确列和标题的CSV格式,但我不想将其作为附件发送。

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

1 个答案:

答案 0 :(得分: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