我不擅長 html 我正在嘗試在 Outlook 電子郵件中創建一個表格,如下所示(沒有顏色)

我嘗試了下面的代碼,但無法獲得這種格式
$HtmlTable = "<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:12px;font-weight: normal;background: #FFFFFF'>
<th align=left><b>Customer Name*</b></th>
<th align=left><b>SERVER NAME</b></th>
<th align=left><b>IP Address</b></th>
<th align=left><b>Hostname without FQDN</b></th>
<th align=left><b>Server OS flavour (Drop Down)</b></th>
<th align=left><b>Datacenter</b></th>
</tr>"
$HtmlTable = "<tr style='font-size:12px;background-color:#FFFFFF'>
<td>" "MYCOMPANY" "</td>
<td>" "TESTSERVER" "</td>
<td>" "10.10.10.10" "</td>
<td>" "test" "</td>
<td>" "test1" "</td>
<td>" "test2" "</td>
</tr>"
$HtmlTable = "</table>"
Send-MailMessage -from $fromEmail -to $toEmail -Cc $toCCmail1 -SmtpServer $smtpServer -Body $HtmlTable -Subject " Report" -BodyAsHtml
在電子郵件中輸出

我喜歡的資料如下
$VM = get-vm | where {($_.Name -match $hostname)} | SELECT -Unique
$Summary = $VM | select @{N="DNSName";E={$_.ExtensionData.Guest.Hostname}}, @{N="IPAddress";E={$_.Guest.IPAddress[0]}}, @{N=”Datacenter”;E={Get-Datacenter -VM $_}}
$DNS = $Summary.DNSName
$pos = $DNS.IndexOf(".")
$DNSName = $DNS.Substring(0, $pos)
$GuestOS = $VM.Guest.OSFullName
$IPAddress = $Summary.IPAddress
$dc = $Summary.Datacenter
請需要您的幫助才能獲得此 html 格式
關于@Theo提供的解決方案

電子郵件正文
$body = @"
<html>
Hi Team, <br /><br />
Please raise a ticket CPU & memory situation from below servers. <br /><br />
<b>Unmapp (Remove) below Situations -</b><br />
MEMORY_USED_WARN<br />
MEMORY_USED_CRIT<br />
CPU_Warning<br />
<br /><br />
$htmlTemplate -replace '@@TABLES@@', ($tables -join '<br />')
Thank you <br /><br />
***This is an auto-generated email. Please do not respond***
</html>
"@
uj5u.com熱心網友回復:
由于這個問題都是關于 HTML 樣式的,我會使用帶有占位符字串的 Here-String 模板來構建正文:
# the first one is the template for the completed body:
$htmlTemplate = @"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
body {font-family: Calibri, Tahoma, Helvetica, sans-serif; color: black;}
table {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;width: 100%;}
td {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
</style>
</head>
<body>
@@TABLES@@
</body>
</html>
"@
# the second one is for each table you want to put in
$tableTemplate = @"
<table>
<tr>
<td>Customer Name</td>
<td colspan="2" style="text-align:center">@@CUSTOMER@@</td>
</tr>
<tr>
<td> </td><td> </td><td> </td>
</tr>
<tr>
<td rowspan="4" style="text-align:center">@@SERVER@@</td>
<td>IP Address</td></td>
<td style="text-align:center">@@IP@@</td>
</tr>
<tr>
<td>Hostname without FQDN</td>
<td style="text-align:center">@@HOSTNAME@@</td>
</tr>
<tr>
<td>Server OS flavour (Drop Down)</td>
<td style="text-align:center">@@OS@@</td>
</tr>
<tr>
<td>Datacenter</td>
<td style="text-align:center">@@DATACENTER@@</td>
</tr>
</table>
"@
現在您使用您的代碼獲取每個 VM 的資訊并回圈訪問該資料。
對于每個 VM,您使用 $tableTemplate 創建一個表:
$tables = foreach ($machine in $VMs) {
# here your code to gather the data
# now replace the placeholders in the template with real data and output
# a completed html table. These will be collected in variable $tables
$tableTemplate -replace '@@CUSTOMER@@', "YourCustomerName" -replace
'@@SERVER@@', "YourServerName" -replace
'@@IP@@', $IPAddress -replace
'@@HOSTNAME@@', $DNSName -replace
'@@OS@@', $GuestOS -replace
'@@DATACENTER@@', $dc
}
之后,將其組合成完整的主體:
$body = $htmlTemplate -replace '@@TABLES@@', ($tables -join '<br />')
并用于-BodySend-MailMessage 呼叫中的引數
您現在應該擁有如下所示的 html:

uj5u.com熱心網友回復:
之前問過同樣的問題,但我不會要求存檔,因為你的文本背景關系不同,所以我們在這里。
我現在不能幫助你,但我會給你足夠有用的鏈接,你可以使用。
這里:
- https://adamtheautomator.com/html-report/
- 在 Powershell 中使用 HTML 創建表
- 甚至 google 類似“使用 PowerShell 的 html 表”之類的東西
如果這不起作用,那么試試這個:( 注意:我使用了相同的問題的答案作為參考)
一張桌子
Get-Content "C:\Dv\Server_List.txt" -ErrorAction SilentlyContinue | ForEach-Object {
Write-Host "Automatic Services Stopped :" $_
Get-WmiObject Win32_Service -ComputerName $_ -Filter "startmode = 'auto' AND state != 'running'"
} |
Select-Object DisplayName, Name, State, StartMode |
ConvertTo-Html |
Out-File C:\Dv\Report.html
多桌
# Generate the content which should be inserted as a set of HTML tables
$preContent = Get-Content "C:\Dv\Server_List.txt" -ErrorAction SilentlyContinue | ForEach-Object {
$ComputerName = $_
Write-Host "Automatic Services Stopped :" $ComputerName
# A table (and heading) will only be generated for $ComputerName if there are services matching the filter.
Get-WmiObject Win32_Service -ComputerName $ComputerName -Filter "startmode = 'auto' AND state != 'running'" |
Select-Object DisplayName, Name, State, StartMode |
ConvertTo-Html -PreContent "<h2>$ComputerName</h2>" -Fragment
}
# Generate the document which holds all of the individual tables.
$htmlDocument = ConvertTo-Html -Head $htmlHead -PreContent $preContent | Out-String
# Because the document has no input object it will have an empty table (<table></table>), this should be removed.
$htmlDocument -replace '<table>\r?\n</table>' | Out-File C:\Dv\Report.html
即使是造型
$HtmlHead = '<style>
body {
background-color: white;
font-family: "Calibri";
}
table {
border-width: 1px;
border-style: solid;
border-color: black;
border-collapse: collapse;
width: 100%;
}
th {
border-width: 1px;
padding: 5px;
border-style: solid;
border-color: black;
background-color: #98C6F3;
}
td {
border-width: 1px;
padding: 5px;
border-style: solid;
border-color: black;
background-color: White;
}
tr {
text-align: left;
}
</style>
# Use the Head parameter when calling ConvertTo-Html
| ConvertTo-Html -Head $HtmlHead |
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/418475.html
標籤:
