top of page
Search

Keep track of those VMware snapshots!

  • ToddimusPrime
  • Dec 15, 2021
  • 2 min read

Have you ever lost track of a VM's snapshot, and before you know it it's suddenly 4TB and your datastore has fallen over? We've all been there... Let's get some automated snapshot reports sent out! Set up this script as a scheduled task or something and get emailed a nice chart of your VMs along with their snapshots. Make sure you've installed two PowerShell modules before you get started: PowerCLI and ReportHTML.


Fill in any info in the following script that's between the < > characters, and away you go!




Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Connect-VIServer -Server <vCenter Server> -User '<vCenter user name goes here>' -Password '<password goes here>'

$vms = Get-VM | Select -ExpandProperty Name
$table = New-Object 'System.Collections.Generic.List[System.Object]'
$reportPath = "C:\scripts\SnapshotReport\" #where to save the report
$reportName = "VMwareSnapshotReport"

foreach ( $vm in $vms )
{
    if ( Get-Snapshot -VM $vm )
    {
        $snapshots = Get-Snapshot -VM $vm | Select-Object VM,Description,SizeGB,Created
        foreach ( $snapshot in $snapshots )
        {
            $obj = [PSCustomObject]@{
		        'VM'				   = $snapshot.VM
		        'Description'	       = $snapshot.Description
		        'Size in GB'		   = [math]::Round($snapshot.SizeGB,2)
		        'Created'			   = $snapshot.Created
            }
            $table.add($obj)
        }
    }
    else
    {
        #no snapshots on VM
        $obj = [PSCustomObject]@{
		    'VM'				   = $vm
		    'Description'	       = "<No snapshots for this VM>"
		    'Size in GB'		   = "N/A"
		    'Created'			   = "N/A"
        }
        $table.add($obj)
    }
}

if ( ($table).Count -eq 0 )
{
    $table = [PSCustomObject]@{
        'Information'		    = 'Error: No VMs found'
    }
}

$sortedTable = $table | Sort-Object -Property VM

$report = New-Object 'System.Collections.Generic.List[System.Object]'
$report += Get-HTMLOpenPage -HideLogos -HideTitle
$report += Get-HTMLContentOpen -HeaderText "Snapshots in vCenter"
$report += Get-HTMLContentDataTable $sortedTable -HideFooter
$report += Get-HTMLContentClose
$report += Get-HTMLClosePage

Save-HTMLReport -ReportContent $report -ReportName $reportName -ReportPath $reportPath

#get the HTML code from the file that was created
$HTMLfile = ("$ReportPath"+"$ReportName"+".html")
$webClient = New-Object System.Net.WebClient
$HTMLcode = $webClient.DownloadString($HTMLfile)

#email the report
$from = '<from address goes here>'
$to = '<to address goes here>'
$subject = 'VMware Snapshot Report'
$body = $HTMLcode
$body += '<h6>Automated report run from scheduled tasks on ServerName</h6>'

$mail = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body
$mail.IsBodyHtml = $true

$server = '<smtp server goes here>'
$port = 25
$smtp = New-Object System.Net.Mail.SmtpClient $server,$port
$smtp.EnableSsl = $true

$smtp.send($mail)


Comments


©2021 by ToddimusPrime

bottom of page