使用 PowerShell 发送的电子邮件中 ​​HTML 无法正确显示

使用 PowerShell 发送的电子邮件中 ​​HTML 无法正确显示

我有一个脚本,它在 HTML 页面上显示所有服务器上的磁盘空间,并并排发送保存的 HTML 文件内容的电子邮件。

HTML 文件是完美的,电子邮件显示与 HTML 文件中相同的表格信息。但是,电子邮件中的表格重叠了……

代码如下:

$freeSpaceFileName = "c:\script\FreeSpace.htm"
$serverlist = "c:\script\computers.txt"
$warning = 50
$critical = 30
New-Item -ItemType file $freeSpaceFileName -Force
# Getting the freespace info using WMI
#Get-WmiObject win32_logicaldisk  | Where-Object {$_.drivetype -eq 3} | format-table DeviceID, VolumeName,status,Size,FreeSpace | Out-File FreeSpace.txt
# Function to write the HTML Header to the file
Function writeHtmlHeader
{
  param($fileName)
  $date = ( get-date ).ToString('dd/MM/yyyy HH:mm:ss')
  Add-Content $fileName "<html>"
  Add-Content $fileName "<head>"
  Add-Content $fileName "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
  Add-Content $fileName '<meta http-equiv="refresh" CONTENT="5">'
  Add-Content $fileName '<title>All Servers DiskSpace Report by Taher Tamboli &copy;</title>'
  add-content $fileName '<STYLE TYPE="text/css">'
  add-content $fileName  "<!--"
  add-content $fileName  "td {"
  add-content $fileName  "font-family: Tahoma;"
  add-content $fileName  "font-size: 20px;"
  add-content $fileName  "border-top: 1px solid #999999;"
  add-content $fileName  "border-right: 1px solid #999999;"
  add-content $fileName  "border-bottom: 1px solid #999999;"
  add-content $fileName  "border-left: 1px solid #999999;"
  add-content $fileName  "padding-top: 0px;"
  add-content $fileName  "padding-right: 0px;"
  add-content $fileName  "padding-bottom: 0px;"
  add-content $fileName  "padding-left: 0px;"
  add-content $fileName  "}"
  add-content $fileName  "body {"
  add-content $fileName  "margin-left: 5px;"
  add-content $fileName  "margin-top: 5px;"
  add-content $fileName  "margin-right: 0px;"
  add-content $fileName  "margin-bottom: 10px;"
  add-content $fileName  ""
  add-content $fileName  "table {"
  add-content $fileName  "border: thin solid #000000;"
  add-content $fileName  "}"
  add-content $fileName  "-->"
  add-content $fileName  "</style>"
  Add-Content $fileName "</head>"
  Add-Content $fileName "<body>"
  add-content $fileName  "<br></br>"
  add-content $fileName  "<table width='100%' align=center>"
  add-content $fileName  "<tr bgcolor='#CCCCCC'>"
  add-content $fileName  "<td colspan='7' height='25' align='center'>"
  add-content $fileName  "<font face='tahoma' color='#003399' size='5'><strong>All Servers DiskSpace Report by Taher Tamboli 2014 &copy; - $date</strong></font>"
  add-content $fileName  "</td>"
  add-content $fileName  "</tr>"
  add-content $fileName  "</table>"
  add-content $fileName  "<br></br>"
}

# Function to write the HTML Header to the file
Function writeTableHeader
{
  param($fileName)

  Add-Content $fileName "<tr bgcolor=#CCCCCC>"
  Add-Content $fileName "<td width='10%' align='center'>Drive</td>"
  Add-Content $fileName "<td width='50%' align='center'>Drive Label</td>"
  Add-Content $fileName "<td width='10%' align='center'>Total Capacity(GB)</td>"
  Add-Content $fileName "<td width='10%' align='center'>Used Capacity(GB)</td>"
  Add-Content $fileName "<td width='10%' align='center'>Free Space(GB)</td>"
  Add-Content $fileName "<td width='10%' align='center'>Freespace %</td>"
  Add-Content $fileName "</tr>"
}

Function writeHtmlFooter
{
  param($fileName)

  Add-Content $fileName "</body>"
  Add-Content $fileName "</html>"
}

Function writeDiskInfo
{
  param($fileName,$devId,$volName,$frSpace,$totSpace)
  $totSpace=[math]::Round(($totSpace/1073741824),2)
  $frSpace=[Math]::Round(($frSpace/1073741824),2)
  $usedSpace = $totSpace - $frspace
  $usedSpace=[Math]::Round($usedSpace,2)
  $freePercent = ($frspace/$totSpace)*100
  $freePercent = [Math]::Round($freePercent,0)
  if ($freePercent -gt $warning)
  {
    Add-Content $fileName "<tr>"
    Add-Content $fileName "<td>$devid</td>"
    Add-Content $fileName "<td>$volName</td>"

    Add-Content $fileName "<td>$totSpace</td>"
    Add-Content $fileName "<td>$usedSpace</td>"
    Add-Content $fileName "<td>$frSpace</td>"
    Add-Content $fileName "<td>$freePercent</td>"
    Add-Content $fileName "</tr>"
  }
  elseif ($freePercent -le $critical)
  {
    Add-Content $fileName "<tr>"
    Add-Content $fileName "<td>$devid</td>"
    Add-Content $fileName "<td>$volName</td>"
    Add-Content $fileName "<td>$totSpace</td>"
    Add-Content $fileName "<td>$usedSpace</td>"
    Add-Content $fileName "<td>$frSpace</td>"
    Add-Content $fileName "<td bgcolor='#FF0000' align=center>$freePercent</td>"
    #<td bgcolor='#FF0000' align=center>
    Add-Content $fileName "</tr>"
  }
  else
  {
    Add-Content $fileName "<tr>"
    Add-Content $fileName "<td>$devid</td>"
    Add-Content $fileName "<td>$volName</td>"
    Add-Content $fileName "<td>$totSpace</td>"
    Add-Content $fileName "<td>$usedSpace</td>"
    Add-Content $fileName "<td>$frSpace</td>"
    Add-Content $fileName "<td bgcolor='#FBB917' align=center>$freePercent</td>"
    # #FBB917
    Add-Content $fileName "</tr>"
  }
}

Function sendEmail
{
  param($from,$to,$subject,$smtphost,$htmlFileName)
  $from=New-Object System.Net.Mail.MailAddress "[email protected]"
  $to= New-Object System.Net.Mail.MailAddress "[email protected]"
  $subject="Servers Disk space report - $Date"
  $smtphost="smtp.google.com"
  $body = Get-Content $htmlFileName
  $smtp= New-Object System.Net.Mail.SmtpClient $smtphost
  $msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body
  $msg.isBodyhtml = $true
  $smtp.Send($msg)
}

writeHtmlHeader $freeSpaceFileName
foreach ($server in Get-Content $serverlist)
{
  Add-Content $freeSpaceFileName "<table width='100%'><tbody>"
  Add-Content $freeSpaceFileName "<tr bgcolor='#CCCCCC'>"
  Add-Content $freeSpaceFileName "<td width='100%' align='center' colSpan=6><font face='tahoma' color='#003399' size='2'><strong> $server</strong></font></td>"
  Add-Content $freeSpaceFileName "</tr>"
  Add-Content $freeSpaceFileName "<br>"
  writeTableHeader $freeSpaceFileName

  $dp = Get-WmiObject win32_logicaldisk -ComputerName $server |  Where-Object {$_.drivetype -eq 3}
  foreach ($item in $dp)
  {
    Write-Host  $item.DeviceID  $item.VolumeName $item.FreeSpace $item.Size
    writeDiskInfo $freeSpaceFileName $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size
  }
}
writeHtmlFooter $freeSpaceFileName
$date = ( get-date ).ToString('yyyy/MM/dd')
#sendEmail [email protected] [email protected] "Disk Space Report - $Date" hub1 $freeSpaceFileName

**####* 点击此处查看错误****###*

答案1

发生这种情况是因为您没有在函数中<table>使用标签来关闭它。</table>writeDiskInfo

为了解决这个问题,在函数右括号前添加以下内容:

Add-Content $fileName "</table>"

因此,你的函数将如下所示:

Function writeDiskInfo
{
 ......
 ......
 if ($freePercent -gt $warning)
 {
 .....
 }
 elseif ($freePercent -le $critical)
 {
 .....
 }
 else
 {
 .....
 }
 Add-Content $fileName "</table>"  # <--- ADD THIS HERE !
}

似乎浏览器对于语法错误的容忍度比邮件客户端更高;)

答案2

KrisFR 说的没错,你需要添加表格末尾的命令,但他提错了。你需要在函数末尾括号之前添加 writeHtmlHeader 函数。

writeHtmlHeader $freeSpaceFileName

foreach ($server 在 Get-Content $serverlist 中)

{

...
...

foreach ($item 在 $dp 中)

{

...
...

}

添加内容 $fileName " /table>" # <--- 在此处添加!

}

相关内容