PowerCLI – vCenter Snapshot Report

A number of my clients have asked for a simple report that can be run against their vCenter to report on VM Snapshots.

Though I would recommend that they use vRealize Operations (vROPS), not every environment has it installed. I have compiled from a number of scripts and forums to tailor the following script. I have taken inspiration from a few of Luc Dekens‘s scripts. I have tested this on vSphere 6.7 and 7.0

$vCenter = "vcsa01.baylyparker.local"
$vCenterUser = "baylyparker\ServiceAccount1"
$vCenterPass = "VMware1!"
$SMTPServer = "smtp.baylyparker.local"
$To = "admin@baylyparker.local"
$From = "vcsa01@baylyparker.local"

#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"
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 = @"
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

The important part is that the variables are changed to match your environment and that the email server is configured to accept unauthenticated emails from where the script is being run from.

The script I suggest is run from a location that you can schedule the script to run on a regular basis (eg. Task Scheduler for Microsoft Windows Server)

Please note that this sample script uses plain text passwords. Using plain text in a production environment is not recommended. You can adapt the script to encrypt the passwords which is not covered in this post.

Be First to Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.