PowerShell Display Table In HTML Email

后端 未结 7 2364
春和景丽
春和景丽 2021-01-02 12:17

Ok, this may have been answered, but I\'m new and trying to learn, so I guess I\'m not \"getting\" it. I have a variable that contains a table of information (I got it from

7条回答
  •  粉色の甜心
    2021-01-02 12:21

    Here's how I'd do it:

    # Create a DataTable
    $table = New-Object system.Data.DataTable "TestTable"
    $col1 = New-Object system.Data.DataColumn Name,([string])
    $col2 = New-Object system.Data.DataColumn Dept,([string])
    $table.columns.add($col1)
    $table.columns.add($col2)
    
    # Add content to the DataTable
    $row = $table.NewRow()
    $row.Name = "John"
    $row.Dept = "Physics"
    $table.Rows.Add($row)
    $row = $table.NewRow()
    $row.Name = "Susan"
    $row.Dept = "English"
    $table.Rows.Add($row)
    
    # Create an HTML version of the DataTable
    $html = ""
    foreach ($row in $table.Rows)
    { 
        $html += ""
    }
    $html += "
    NameDept
    " + $row[0] + "" + $row[1] + "
    " # Send the email $smtpserver = "smtpserver.domain.com" $from = "user@domain.com" $to = "user@domain.com" $subject = "test" $body = "Hi there,
    Here is a table:

    " + $html Send-MailMessage -smtpserver $smtpserver -from $from -to $to -subject $subject -body $body -bodyashtml

提交回复
热议问题