Patrick
Patrick

Reputation: 127

Converting Hashtable in Powershell

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 protected]" -from "Automated Reboot<[email protected]>" -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.

Upvotes: 0

Views: 5413

Answers (1)

mjolinor
mjolinor

Reputation: 68263

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

Upvotes: 1

Related Questions