Consolidate snapshots in vSphere 5 with PowerCLI

Consolidate snapshots in vSphere 5 with PowerCLI

Consolidate snapshots in vSphere 5 with PowerCLI

Use PowerCLI to consolidate snapshots in vSphere 5

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.

1
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.

1
(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:

1
(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.

1
2
3
4
5
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.

Comments are closed.