Use PowerCLI to consolidate snapshots in vSphere 5
September 26, 2012 7 Comments
In vSphere 5 a virtual machine can have a “Virtual machine disks consolidation is needed” Configuration Issue warning in the Summary tab. How can you use PowerCLI to see which virtual machines have this warning? And how can you automate the consolidation of the virtual machine’s disks?
List virtual machines that need disks consolidation
The PowerCLI command in listing 1 will return all the virtual machines that need disk consolidation.
Get-VM | Where-Object {$_.Extensiondata.Runtime.ConsolidationNeeded}
Listing 1. PowerCLI command to list all virtual machines that need disk consolidation.
Consolidate a virtual machine’s disks
The PowerCLI command in listing 2 will consolidate the disks of a virtual machine called MyVM. The command will not wait untill the consolidation is finished but will return immediately.
(Get-VM -Name "MyVM").ExtensionData.ConsolidateVMDisks_Task()
Listing 2. PowerCLI command to consolidate the disks of a virtual machine called MyVM.
If you want to wait until the task is finished before continuing with your PowerCLI script, you need to use the ConsolidateVMDisks method:
(Get-VM -Name "MyVM").ExtensionData.ConsolidateVMDisks()
Listing 3. PowerCLI command to consolidate the disks of a virtual machine called MyVM and wait untill the task is finished.
Consolidate the disks of all virtual machine’s that need it
When you want to consolidate the disks of all virtual machines that need disks consolidation then you can use the script from listing 4.
Get-VM | Where-Object {$_.Extensiondata.Runtime.ConsolidationNeeded} | ForEach-Object { $_.ExtensionData.ConsolidateVMDisks() }
Listing 4. PowerCLI script to consolidate the disks of all virtual machines that need disks consolidation. The script will wait untill the consolidation of the disks of a virtual machine is finished before continuing with the next virtual machine.
Link
The following VMWare Knowledge Base article will give you more information about consolidating snapshots in vSphere 5:
Consolidating snapshots in vSphere 5
Nice !
Awesome, thanks for this.
Pingback: PowerCLI: Consolidate all VMs that need consolidation | cloud.kemta.net
very good and handy tool
thanks a lot
{$_.Extensiondata.Runtime.ConsolidationNeeded}
Where do you get that information from ^^^ ???
I am curious how I can leverage the shell to provide information such as this.
Nice work – helped greatly in consolidating a lot of VM’s.
You could also add a wait-state if you want to have the multi-threaded “ExtensionData.ConsolidateVMDisks_Task()” but want to restrain it a bit. Here’s what I did:
Get-VM |
# find VM’s in need of consolidation
Where-Object {$_.Extensiondata.Runtime.ConsolidationNeeded} |
ForEach-Object {
# echo hostname found
Write-host $_
# consolidate disks
$_.ExtensionData.ConsolidateVMDisks_Task()
# input a wait state, then continue finding next VM
# Write-host “pausing 60 seconds…”
sleep 60
}
This script works great in the latest powerCLI tools as of October 2015. (ver 5.8 Release 1)
Joel
Hi Joel,
I found this very useful, looking for script to consolidate lists of VM’s that are alerted on VC for Disk Consolidation.
VC -6.0 and ESXI host with 5.5.