我有一个PowerShell哈希变量,如下所示
Name Value
a 1
b 2
c 3
我希望以以下格式发送电子邮件
员工详细信息
EmpName EmpID
a 1
b 2
c 3
如何以上述格式从azure Runbook发送电子邮件。
注意:我正在使用sendgrid发送电子邮件
您正在将HTML正文创建为字符串数组,并且该数组中的第一行同时打开并关闭html。 (<html>...</html>
)。恐怕这看起来不太好。
我愿意
ConvertTo-Html
cmdlet将其转换为html表。 $body
变量中。代码:
$Emp= @{a=1;b=2;c=3}
# create a html table of the data in the hash
$table = $Emp.GetEnumerator() |
Select-Object @{Name = 'EmpName'; Expression = {$_.Key}},
@{Name = 'EmpID'; Expression = {$_.Value}} |
Sort-Object EmpName |
ConvertTo-Html -Fragment
# create the complete html body
$body = "<html><h2>Employee Details</h2><br>$table<html>"
我在下面的脚本中使用过,但是输出与预期不符
$Emp=@{a=1;b=2;c=3}
$body = @()
$body += "<html><h2>Employee Details</h2><br></html>"
$body += $Emp | format-table -autosize
$body = $body | out-string
Getting Email like below:
**Employee Details**
name value ------ ------ a 1 b 2 c 3