问题
I'm using a powershell script that will create an HTML report of new deployed VM and send it as an email. I have tried a lot of stuff so far. But no luck. Unfortunately , I am unable to get mail. Where am I wrong ? Here are the relevant parts of the script...
$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
Last update :
But When I run each time script , I am getting duplicate mail like below. it sounds like these values appends into the variable.
Mail body :
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
回答1:
There are a few things I feel are not right.
The helper function Create-HTMLTable
is called with parameter $reports
, but that is a typo, because the variable is actually called $Report
.
Also, the function creates html from an array using ConvertTo-Html
without the -Fragment
switch and then tries to remove the extra html that is put in.
When using the -Fragment
switch, there would be no need for that.
Next, when building the $Report, you are using $row.ServerName
, but that seems never defined.
Try:
$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
The HTML output should look something like this:
The reason for creating two HTML templates is that we want to be able to use placeholders '{0}' and '{1}' and replace these later using the -f
Format operator. Because in the first part there are style definitions, also using {
and }
characters, if we do this in just one template, all of these existing curly braces would need to be doubled, otherwise -f
will not be able to find and replace the placeholders.
来源:https://stackoverflow.com/questions/61790646/powershell-unable-to-send-mail-html-message