Whether your developers are keeping snapshots around for months or your backup program is leaving behind old snapshots due to backup failures, its always good to know about your aging snapshots.
Below is a script which will find all VMs with snapshots older than 7 days. As usual you can customize this to meet your needs, for me I like to keep transcripts and output files for archival and troubleshooting.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
$currentdate = (Get-Date).tostring("yyyy-MM-dd") $transfilename = 'OldSnapsTranscript_' + $currentdate + '.txt' $transfilepath = 'C:\scripts\Transcripts\' + $transfilename Start-Transcript $transfilepath -NoClobber -Append #Load PowerCLI snapins and dot source them Add-PSSnapin vmware.vimautomation.core . "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1" Connect-VIServer vcenterserver.company.com #Generating timestamped log name for powercli html output $7daysago = (Get-Date).AddDays(-7) $outfilename = 'Old_Snaps_' + $currentdate + '.htm' $outfilepath = 'C:\scripts\Output\' + $outfilename $vmswithstalesnaps = Get-VM | Get-Snapshot | Where-Object {$_.created -lt $7daysago } | Select-Object vm,Name,Created,@{Name="MbSize";Expression={[math]::Truncate($_.SizeMb)}} #Convert it to html and save the output if it exists If ($vmswithstalesnaps) { $vmswithstalesnaps | Sort-Object created | ConvertTo-Html | Out-File $outfilepath } #variables for email output $eto = "youremail@company.com" $efrom = "PowerCLI_Reports <PowerCLIReports@company.com>" $esubject = "Virtual Machine Stale SnapShot Report" If ($vmswithstalesnaps) { $ebody = (Get-Content $outfilepath | Out-String) } $eserver = "smtpserver.company.com" #check the variable for a value to determine which email to send If ($vmswithstalesnaps) { Send-MailMessage -From $efrom -To $eto -Subject $esubject -BodyAsHtml -Body $ebody -smtpserver $eserver } Else { Send-MailMessage -From $efrom -To $eto -Subject $esubject -Body "There are no VMs with snapshots older than 7 days in this report" -smtpserver $eserver } |