PowerShell script not sending HTML Report via windows task Scheduler but it works when i run script Manually Please check the below codes.
$Snapshots = Get-VM | Get-Snapshot | Where-Object {$_.Name -notmatch "Restore Point" -And $_.Name -notmatch "Veeam Replica Working Snapshot" -And $_.Name -notmatch "VEEAM BACKUP TEMPORARY SNAPSHOT"} | select name,Description,Created,VM,SizeMB,SizeGB | Sort-Object -Property Name
function Get-SnapshotSize ($Snapshot)
{
if ($snapshot.SizeGB -ge "1")
{
$Snapshotsize = [string]([math]::Round($snapshot.SizeGB,3)) + " GB"
}
else {
$Snapshotsize = [string]([math]::Round($snapshot.SizeMB,3)) + " MB"
}
Return $Snapshotsize
}
function set-SnapshotDate ($snapshot)
{
$RedValue = (get-date).AddDays(-10)
$greenValue = (get-date).AddDays(-5)
if ($snapshot.created -gt $greenValue)
{
$backgroundcolor = "green"
}
elseif ($snapshot.Created -lt $greenValue -and $snapshot.Created -gt $RedValue)
{
$backgroundcolor = "yellow"
}
else
{
$backgroundcolor = "red"
}
return $backgroundcolor
}
function Format-HTMLBody ($body)
{
$newbody = @()
foreach ($line in $body)
{
## Remove the Format Header
if ($line -like "*<th>Format</th>*")
{
$line = $line -replace '<th>Format</th>',''
}
## Format all the Red rows
if ($line -like "*<td>red</td>*")
{
$line = $line -replace '<td>red</td>',''
$line = $line -replace '<tr>','<tr style="background-color:Tomato;">'
}
## Formating all the Yellow Rows
elseif ($line -like "*<td>yellow</td>*")
{
$line = $line -replace '<td>yellow</td>',''
$line = $line -replace '<tr>','<tr style="background-color:Orange;">'
}
## Formating all the Green Rows
elseif ($line -like "*<td>green</td>*")
{
$line = $line -replace '<td>green</td>',''
$line = $line -replace '<tr>','<tr style="background-color:MediumSeaGreen;">'
}
## Building the new HTML file
$newbody += $line
}
return $newbody
}
$date = (get-date -Format d/M/yyyy)
$header = @"
<style>
TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: black;}
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
body { font-family: Calibri,Candara,Segoe,Segoe UI,Optima,Arial,sans-serif;
font-size: 14px;
line-height: 10px;
font-weight: 400;
color: black;
}
</style>
"@
$PreContent = "<H4> Crestmark Vmware Virtual Machines Snapshots. Green color tab represents within 5 days old snapshot red represnets more than 10 days old snapshot yellow represents between 5 days to 10 days.This report excluded the snapshot if the name match with Restore Point,Veeam Replica Working Snapshot,Veeam Backup Temporary Snapshot. </H4>"
$html = $Snapshots | select VM,name,Created,@{Label="Size";Expression={Get-SnapshotSize($_)}},Description,@{Label="Format";Expression={set-SnapshotDate($_)}}| sort Created -Descending | ConvertTo-Html -Head $header -PreContent $PreContent
$Report = Format-HTMLBody ($html)
$MessageContent = ("$Report")
$MessageContent+= @"
<br><Font face="calibri">Regards,<br /><br />
<font size="4"><b>Crestmark VMware </b></font><br />
<br>System Administration Team<br />
</font>
"@
## MailParam
$MailParam = @{
To = "raj@test.com"
From = "raj@test.com"
SmtpServer = "mailgateway.internal.xxxx.com"
Subject = "VMware Snapshot Report for " + (get-date -Format d/M/yyyy)
body = ("$MessageContent")
}
## Sending the Email
Send-MailMessage @MailParam -BodyAsHtml
Disconnect-VIServer -Server * -Force -Confirm:$false
That is probably not related to the script itself, but to the way you defined the scheduled task.
Most of the time this is the account under which the scheduled task runs.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference