Use PowerCLI to consolidate snapshots in vSphere 5

PowerCLI logoIn 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

About these ads

About Robert van den Nieuwendijk
Robert van den Nieuwendijk is a freelance senior systems engineer with over 25 years of experience in the IT industry. His main focus is VMware vSphere and Microsoft Windows Server. He tries to automate as much of his work as possible using Microsoft Windows PowerShell. Robert is the author of the book “Learning PowerCLI”. Robert is a frequent contributor and moderator at the VMware VMTN Communities. He has a bachelor degree in software engineering and holds the following IT certifications and accreditations: VSP 5, VTSP 5, VCP4-DCV, VCP5-DCV, VCA-Cloud, VCA-WM, MCSE, MCSA, MCP, MCP+I, and ITIL Foundation. In 2012, 2013 and in 2014 Robert received the VMware vExpert title for his contribution to the community of VMware users over the past year.

3 Responses to Use PowerCLI to consolidate snapshots in vSphere 5

  1. Glenn says:

    Awesome, thanks for this.

  2. Pingback: PowerCLI: Consolidate all VMs that need consolidation | cloud.kemta.net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 773 other followers

%d bloggers like this: