Converting Hashtable in Powershell

强颜欢笑 提交于 2019-12-08 12:18:42

问题


I need some help outputting a a hash table to the body of an email message.

$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

The Message body looks like it should if i just returned the $table

I would ideally like to convert the table to a format that looks like a CSV with the proper columns and headers but I don't want to email it as an attachment.

Does anyone know how i can go about doing this? I would even considering converting it to HTML so it looks nice in the email.


回答1:


Using 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

for HTML:

[PSCustomObject]$table | convertto-html


来源:https://stackoverflow.com/questions/19455435/converting-hashtable-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!