Home > database >  HTML error with creating VM snapshot report to email it to my self
HTML error with creating VM snapshot report to email it to my self

Time:08-09

I got the below PS script Idea online to create a VM Snapshot report and email it to myself every morning, once I run the script it will get the below error message, can someone help me with that? Thanks

#Variables
$vCenter = "qwe-2019.wer.com"
$vCenterUser = "wer\adm-myuser"
$vCenterPass = "balabala"
$SMTPServer = "***********.com"
$To = "************.com"
$From = "***********.com"

#HTML formatting
$style = "BODY{font-family: Arial; font-size: 10pt;}" $style = $style   "TABLE{border: 1px solid red; border-collapse: collapse;}" $style = $style   "TH{border: 1px solid; background-color: #4CAF50; color: white; padding: 5px; }" $style = $style   "TD{border: 1px solid; padding: 5px; }" $style = $style   ""
$date = Get-Date -Format "dddd dd/MM/yyyy HH:mm K"

#Connect to vCenter"
CLS
Write-Host "Connecting to $vCenter" -ForegroundColor Blue
Connect-VIServer -Server $vCenter -User $vCenterUser -Password $vCenterPass -Force | Out-Null
Write-Host " Connected to $vCenter" -ForegroundColor Green

#Get list of VMs with snapshots
Write-Host "Generating VM snapshot report" -ForegroundColor Blue
$SnapshotReport = Get-vm | Get-Snapshot | Select-Object VM,Description,PowerState,SizeGB | Sort-Object SizeGB | ConvertTo-Html -Head $style | Out-String
Write-Host " Completed" -ForegroundColor Green

#Sending email report
Write-Host "Sending VM snapshot report" -ForegroundColor Blue
$htmlbody = @"
$SnapshotReport
"@
Send-MailMessage -smtpserver $SMTPServer -From $From -To $To -Subject "Snapshot Email Report for $Date" -BodyAsHtml -Body $htmlbody
Write-Host " Completed" -ForegroundColor Green

#Disconnecting vCenter
Disconnect-VIServer -Server $vCenter -Force -Confirm:$false
Write-Host "Disconnecting to $vCenter" -ForegroundColor Blue

**Error message from #HTML formatting section is **

At line:10 char:55
  $style = "BODY{font-family: Arial; font-size: 10pt;}" $style = $style ...
                                                        ~~~~~~
Unexpected token '$style' in expression or statement.
At line:10 char:132
  ... LE{border: 1px solid red; border-collapse: collapse;}" $style = $styl ...
                                                             ~~~~~~
Unexpected token '$style' in expression or statement.
At line:10 char:231
  ... kground-color: #4CAF50; color: white; padding: 5px; }" $style = $styl ...
                                                             ~~~~~~
Unexpected token '$style' in expression or statement.
At line:10 char:289
  ... yle = $style   "TD{border: 1px solid; padding: 5px; }" $style = $styl ...
                                                             ~~~~~~
Unexpected token '$style' in expression or statement.
      CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
      FullyQualifiedErrorId : UnexpectedToken

CodePudding user response:

As commented, use a Here-String to create the style part of the Html.
Also, don't forget to enclose it in <style>..</style> tags.

Then for cmdlets like Send-MailMessage that potentially take a lot of parameters, I would use Splatting

$style = @'
<style>
    body {font-family: Arial; font-size: 10pt;}
    table {border: 1px solid red; border-collapse: collapse;}
    th {border: 1px solid; background-color: #4CAF50; color: white; padding: 5px;}
    td {border: 1px solid; padding: 5px;}
</style>
'@

$SnapshotReport = Get-Vm | Get-Snapshot | Select-Object VM,Description,PowerState,SizeGB | 
                  Sort-Object SizeGB | ConvertTo-Html -Head $style | Out-String

#Sending email report
Write-Host "Sending VM snapshot report" -ForegroundColor Blue

# code is nicer/better maintainable if you use splatting
$params = @{
    SmtpServer = $SMTPServer
    From       = $From
    To         = $To
    Subject    = "Snapshot Email Report for $Date"
    Body       = $SnapshotReport
    BodyAsHtml = $true
    # more parameters can go here
}

Send-MailMessage @params

Write-Host " Completed" -ForegroundColor Green
  • Related