如何通过表格格式通过表格发送电子邮件哈希数据,并带有来自Azure Powershell Runbook的标题

问题描述 投票:0回答:2

我有一个PowerShell哈希变量,如下所示

Name         Value
 a             1
 b             2
 c             3

我希望以以下格式发送电子邮件

员工详细信息

  EmpName     EmpID
    a          1
    b          2
    c          3

如何以上述格式从azure Runbook发送电子邮件。

注意:我正在使用sendgrid发送电子邮件

azure powershell azure-automation azure-runbook
2个回答
0
投票

您正在将HTML正文创建为字符串数组,并且该数组中的第一行同时打开并关闭html。 (<html>...</html>)。恐怕这看起来不太好。

我愿意

  1. 使用计算的属性将哈希表转换为对象数组以获取所需的标头(EmpName和EmpID)
  2. 对'EmpName'属性进行排序(不对哈希进行排序,但这可以解决此问题)
  3. 使用ConvertTo-Html cmdlet将其转换为html表。
  4. 最后将它们全部合并到$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>"

0
投票

我在下面的脚本中使用过,但是输出与预期不符

$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
© www.soinside.com 2019 - 2024. All rights reserved.