如何通过Azure Powershell Runbooks的标题以表格格式通过电子邮件发送哈希数据

我有一个如下所示的PowerShell Hash变量

Name         Value
 a             1
 b             2
 c             3

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

员工详细信息

  EmpName     EmpID
    a          1
    b          2
    c          3

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

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

zsandy 回答:如何通过Azure Powershell Runbooks的标题以表格格式通过电子邮件发送哈希数据

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

$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
,

您正在将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>"
本文链接:https://www.f2er.com/2971169.html

大家都在问