Converting Hashtable in Powershell

2019-06-14 01:45发布

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条回答
何必那么认真
2楼-- · 2019-06-14 02:30

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
查看更多
登录 后发表回答