Powershell - 无法发送邮件 HTML 消息
Powershell - Unable to send mail HTML message
我正在使用 powershell 脚本,该脚本将创建新部署的 VM 的 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
最后更新:
但是当我 运行 每次编写脚本时,我都会收到如下所示的重复邮件。
听起来这些值附加到变量中。
邮件正文:
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
有几点感觉不对。
辅助函数 Create-HTMLTable
是用参数 $reports
调用的,但这是一个错字,因为该变量实际上被称为 $Report
.
此外,该函数使用不带 -Fragment
开关的 ConvertTo-Html
从数组创建 html,然后尝试删除放入的额外 html。
使用 -Fragment
开关时,就不需要那个了。
接下来,在构建 $Report 时,您正在使用 $row.ServerName
,但似乎从未定义过。
尝试:
$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
HTML 输出应如下所示:
创建两个 HTML 模板的原因是我们希望能够使用占位符“{0}”和“{1}”,稍后使用 -f
格式运算符。因为在第一部分中有样式定义,也使用 {
和 }
字符,如果我们只在一个模板中这样做,所有这些现有的花括号都需要加倍,否则 -f
将无法找到并替换占位符。
我正在使用 powershell 脚本,该脚本将创建新部署的 VM 的 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
最后更新:
但是当我 运行 每次编写脚本时,我都会收到如下所示的重复邮件。 听起来这些值附加到变量中。
邮件正文:
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
有几点感觉不对。
辅助函数 Create-HTMLTable
是用参数 $reports
调用的,但这是一个错字,因为该变量实际上被称为 $Report
.
此外,该函数使用不带 -Fragment
开关的 ConvertTo-Html
从数组创建 html,然后尝试删除放入的额外 html。
使用 -Fragment
开关时,就不需要那个了。
接下来,在构建 $Report 时,您正在使用 $row.ServerName
,但似乎从未定义过。
尝试:
$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
HTML 输出应如下所示:
创建两个 HTML 模板的原因是我们希望能够使用占位符“{0}”和“{1}”,稍后使用 -f
格式运算符。因为在第一部分中有样式定义,也使用 {
和 }
字符,如果我们只在一个模板中这样做,所有这些现有的花括号都需要加倍,否则 -f
将无法找到并替换占位符。