PowerShell Display Table In HTML Email

后端 未结 7 2319
春和景丽
春和景丽 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 = "<table><tr><td>Name</td><td>Dept</td></tr>"
    foreach ($row in $table.Rows)
    { 
        $html += "<tr><td>" + $row[0] + "</td><td>" + $row[1] + "</td></tr>"
    }
    $html += "</table>"
    
    # Send the email
    $smtpserver = "smtpserver.domain.com"
    $from = "user@domain.com"
    $to = "user@domain.com"
    $subject = "test"
    $body = "Hi there,<br />Here is a table:<br /><br />" + $html
    Send-MailMessage -smtpserver $smtpserver -from $from -to $to -subject $subject -body $body -bodyashtml
    
    0 讨论(0)
  • 2021-01-02 12:28

    To build on user1491315 answer.

    $html = "<table border='1'><tr>"
    $tableCol = $table.Columns | select Caption
    foreach($col in $tableCol){
        $html += "<td>" + $col.Caption + "</td>"
    }
        $html += "</tr>"
    $tableColCount = $table.Columns.Count
    foreach ($row in $table.Rows)
    {
        $html += "<tr>"
        For ($i=0; $i -le $tableColCount; $i++) {
            $html += "<td>" + $row[$i] + "</td>"
        }
        $html += "</tr>"
    }
    $html += "</table>"
    

    This does the same thing but will automatically set column names.

    0 讨论(0)
  • 2021-01-02 12:29

    I use like this:

    Import-Module C:\dbatools\dbatools.psd1
    
    $HostName = Get-WMIObject Win32_ComputerSystem | Select-Object -ExpandProperty name
    $DiskResult = Get-DbaDiskSpace -ComputerName $HostName | Sort-Object $_.FreeInGB | Out-DbaDataTable
    
    # Create column headers of Table1
    $HtmlTable1 = "<table border='1' align='Left' cellpadding='2' cellspacing='0' style='color:black;font-family:arial,helvetica,sans-serif;text-align:left;'>
    <tr style ='font-size:13px;font-weight: normal;background: #FFFFFF'>
    <th align=left><b>ComputerName</b></th>
    <th align=left><b>Name</b></th>
    <th align=left><b>Label</b></th>
    <th align=left><b>SizeInGB</b></th>
    <th align=left><b>FreeInGB</b></th>
    <th align=left><b>PercentFree</b></th>
    </tr>"
    
    # Insert data into Table1
    foreach ($row in $DiskResult)
    { 
        $HtmlTable1 += "<tr style='font-size:13px;background-color:#FFFFFF'>
        <td>" + $row.ComputerName + "</td>
        <td>" + $row.Name + "</td>
        <td>" + $row.Label + "</td>
        <td>" + $row.SizeInGB + "</td>
        <td>" + $row.FreeInGB + "</td>
        <td>" + $row.PercentFree + "</td>
        </tr>"
    }
    $HtmlTable1 += "</table>"
    
    # Send Mail Inputs
    $smtpserver = "smtp.domain.com"
    $from = "Powershell Mail <" + $HostName + "@domain.com>"
    $to = "<mail1@domain.com>", "<mail2@domain.com>"
    $cc = "<mail3@domain.com>" , "<mail4@domain.com>"
    $subject = "Disk Check for SQL Server"
    $body = "Disk info for SQL Server like below:<br/><br/>" + $HtmlTable1
    
    Send-MailMessage -smtpserver $smtpserver -from $from -to $to -cc $cc -subject $subject -body $body -bodyashtml
    
    0 讨论(0)
  • 2021-01-02 12:32

    You can try (not tested):

    $message.Body = $message.Body + ($DataSet.Tables | format-Table -auto |  convertto-html)
    
    0 讨论(0)
  • 2021-01-02 12:40

    generalized answer:

    $body+=($variable|Select-Object property1,property2|ConvertTo-Html)

    just use you own variables and properties. When other people search for solutions, this is what we want to see.

    0 讨论(0)
  • 2021-01-02 12:45

    For someone looking for a simple table,here is what i have found

    add below variable first

    $Header = @"
    <style>
    TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
    TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;}
    TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
    </style>
    "@
    

    You can add like below then

    Get-PSDrive | ConvertTo-Html -Property Name,Used,Provider,Root,
    CurrentLocation 
    -Head $Header | Out-File -FilePath PSDrives.html
    

    While reading the file,ensure you use -raw as a parameter like bellow

    Get-ChildItem Cert:\LocalMachine\My  | Where-Object {$_.NotAfter -ge (get-date -DisplayHint Date) } |ConvertTo-Html -Property Issuer,Thumbprint,Notafter -Head $Header | Out-File C:\Iris\Cert.html
    $body=Get-Content -Path C:\Iris\cert.html -Raw
    

    then while sending mail, you can do

    send-emailmessage -body $body -bodyashtml #other values
    

    References:
    https://4sysops.com/archives/building-html-reports-in-powershell-with-convertto-html/

    0 讨论(0)
提交回复
热议问题