Powershell-无法发送邮件HTML消息
我正在使用一个powershell脚本,它将创建新部署VM的HTML报告,并将其作为电子邮件发送。 到目前为止,我已经尝试了很多东西。但是没有运气。不幸的是,我无法收到邮件。我错在哪里? 以下是脚本的相关部分Powershell-无法发送邮件HTML消息,html,css,powershell,sendmail,Html,Css,Powershell,Sendmail,我正在使用一个powershell脚本,它将创建新部署VM的HTML报告,并将其作为电子邮件发送。 到目前为止,我已经尝试了很多东西。但是没有运气。不幸的是,我无法收到邮件。我错在哪里? 以下是脚本的相关部分 $Date = get-date $Datefile = ( get-date ).ToString(‘yyyy-MM-dd-hhmmss’) $ErrorActionPreference = "SilentlyContinue" # Variable to change $HTML =
$Date = get-date
$Datefile = ( get-date ).ToString(‘yyyy-MM-dd-hhmmss’)
$ErrorActionPreference = "SilentlyContinue"
# Variable to change
$HTML = "yes"
#Add Text to the HTML file
Function Create-HTMLTable
{
param([array]$Array)
$arrHTML = $Array | ConvertTo-Html
$arrHTML[-1] = $arrHTML[-1].ToString().Replace(‘</body></html>’,"")
Return $arrHTML[5..2000]
}
$Header = "
<html><head></head><body>
<style>table{border-style:solid;border-width:1px;font-size:8pt;background-color:#ccc;width:100%;}th{text-align:left;}td{background-color:#fff;width:20%;border-style:so
lid;border-width:1px;}body{font-family:verdana;font-size:12pt;}h1{font-size:12pt;}h2{font-size:10pt;}</style>
<H1>VMware VM information</H1>
<H2>Date and time</H2>,$date
"
$Report = @()
Get-VM $row.ServerName | %
{
$vm = Get-View $_.ID
$vms = "" | Select-Object VMName, Hostname, IPAddress
$vms.VMName = $vm.Name
$vms.Hostname = $vm.guest.hostname
$vms.IPAddress = $vm.guest.ipAddress
$Report += $vms
}
if ($HTML -eq "yes") {
$output += ‘<p>’
$output += ‘<H2>VMware VM information</H2>’
$output += ‘<p>’
$output += Create-HTMLTable $reports
$output += ‘</p>’
$output += ‘</body></html>’ }
Send-MailMessage -to $emailto -Subject $subject -SmtpServer $smtp -From $fromaddress -Body ($output) -BodyAsHtml
有几件事我觉得不对 助手函数Create HTMLTable是使用参数$reports调用的,但这是一个输入错误,因为该变量实际上被称为$Report。 此外,该函数使用converttohtml从数组中创建html,而不使用-Fragment开关,然后尝试删除放入的额外html。 当使用-Fragment开关时,不需要这样做 接下来,在构建$Report时,您将使用$row.ServerName,但这似乎从未定义过 尝试: HTML输出应如下所示:
创建两个HTML模板的原因是我们希望能够使用占位符“{0}”和“{1}”,并在以后使用-f Format操作符替换它们。因为在第一部分中有样式定义,也使用了{和}字符,如果我们只在一个模板中执行此操作,所有现有的大括号都需要加倍,否则-f将无法找到并替换占位符。首先检查是否可以使用send MailMessage发送任何邮件。可能SMTP服务器拒绝了连接尝试。如果我作为附件发送,它可以工作,但不能作为正文。原因之一是:理顺引号。您现在使用的是像“and”这样的卷发。在Word文档中,它们可能看起来不错,但在代码中,它们可能意味着很大的麻烦。正如您所说,我再次编辑了这些。但是没有luck@Theo,你推荐什么?嗨,提奥,我有新的一期。你有机会看一看吗?
VMware VM information
Date and time
05/14/2020 17:24:51
VMware VM information
VMName, Hostname, IPAddress
VM01, Vm01 , xx.xx.xx.xx
VM02, Vm02 , xx.xx.xx.xx
VMware VM information
VMName, Hostname, IPAddress
VM01, Vm01 , xx.xx.xx.xx
VM02, Vm02 , xx.xx.xx.xx
$Date = Get-Date
$Datefile = '{0:yyyy-MM-dd-hhmmss}' -f $Date # not sure why you need this
$ErrorActionPreference = "SilentlyContinue"
# Variable to change. Make this a Boolean, so it can be used directly for the `BodyAsHTML` switch
$HTML = $true
# create Here-String templates for the HTML and for a plain-text output
$htmlBegin = @"
<html><head></head><body>
<style>
table{border-style:solid;border-width:1px;font-size:8pt;background-color:#ccc;width:100%;}
th{text-align:left;}td{background-color:#fff;width:20%;border-style:solid;border-width:1px;}
body{font-family:verdana;font-size:12pt;}h1{font-size:12pt;}h2{font-size:10pt;}
</style>
<H1>VMware VM information</H1>
"@
# the placeholders '{0}' and '{1}' will be filled in later
$htmlEnd = @"
<H2>Date and time: {0}</H2>
<p></p>
<p>{1}</p>
</body></html>
"@
$plainText = @"
VMware VM information
Date and time: {0}
{1}
"@
# get the report for the VMs
$Report = Get-VM | ForEach-Object {
Get-View $_.ID | Select-Object @{Name = 'VMName'; Expression = { $_.Name }},
@{Name = 'Hostname'; Expression = { $_.guest.hostname }},
@{Name = 'IPAddress'; Expression = { $_.guest.ipAddress }}
}
if ($HTML) {
# convert the report into a HTML table. Use -Fragment to
# just the HTML for the table; no '</body></html>'
$table = ($Report | ConvertTo-Html -Fragment) -join [Environment]::NewLine
$output = $htmlBegin + ($htmlEnd -f $date, $table)
}
else {
$table = $Report | Format-Table -AutoSize | Out-String
$output = $plainText -f $date, $table
}
# create a Hashtable for splatting the parameters to Send-MailMessage
$mailParams = @{
To = $emailto
From = $fromaddress
Subject = $subject
SmtpServer = $smtp
Body = $output
BodyAsHtml = $HTML # $true of $false
}
Send-MailMessage @mailParams