Recently we encountered an issue where a team member mistakenly did not delete a snapshot following some maintenance on a VM. Long story short, the snapshot ballooned into more than 2TB and took many hours to consolidate. Aside from the obvious of "don't forget to delete snapshots", we're looking for ideas on ways to prevent this from happening.
One idea that our manager wanted us to pursue was to have a script which would routinely scan for VMs showing the "Disk Consolidation Needed" warning. It would then alert us, and start the consolidation process. This wasn't complicated to write, but I'm having trouble getting a VM into the "Disk Consolidation Needed" state to test it.
Also, any other ideas on better ways to handle this are certainly welcome.
Script code:
# Notes: Since we don't specify a credential pair to connect with, it will attempt to connect as the current script environment's user.
# Load PowerCLI Module
Import-Module -Name VMware.VimAutomation.Core
##### Variables #####
$vCenters = @('XXXX', 'YYYY')
$logpath = "$env:SystemDrive\Windows\Temp\VM_Consolidation.log"
############
<#
.SYNOPSIS
Logs messages to a file
.DESCRIPTION
Receives messages and logs them to an output file.
.PARAMETERmsg
The message to be written
.EXAMPLE
Write-Log 'Write this to the logfile'
.NOTES
Requires $logfile to be configured as the path to the output log file
#>
functionWrite-Log {
param($msg)
"$(Get-Date-Format G) : $msg" | Out-File -FilePath $logpath -Append -Force
}
foreach ($vCenterin$vCenters) {
try {
Connect-VIServer$vCenter -ErrorAction Stop
} catch {
Write-Log'Cannot connect to $vCenter'
}
}
if ($Global:DefaultVIServers.Count -gt 0) {
$VMsNeedingConsolidation = Get-VM | Where-Object {$_.ExtensionData.Runtime.consolidationNeeded}
if ($VMsNeedingConsolidation -gt 0){
Write-Log"VMs needing consolidation: $VMsNeedingConsolidation"
$VMsNeedingConsolidation | ForEach-Object {
Write-Log"Needs consolidation $_ "
$_.ExtensionData.ConsolidateVMDisks_Task()
}
}
}