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 Robert van den Nieuwendijk
Robert van den Nieuwendijk is a freelance senior systems engineer with over 30 years of experience in the IT industry. He focusses on VMware vCloud Suite and Microsoft Windows Server. He tries to automate as much of his work as possible using Microsoft PowerShell. Robert is the author of the books “Learning PowerCLI” and “Learning PowerCLI – Second Edition.” 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 2016, VTSP 2016, VCP4-DCV, VCP5-DCV, VCP6-DCV, VCP6-CMA, VCA-Cloud, VCA-WM, VCA-NV, VMSP, VMTSP, ZCS, ZCP, ZCP-Cloud, MCSE, MCSA, MCP, MCP+I, PRINCE2 Foundation and ITIL Foundation. In 2012, 2013, 2014, 2015, 2016, 2017, 2018 and 2019 Robert received the VMware vExpert award for his contribution to the community of VMware users over the past year. In 2017 Robert also received the VMware vExpert Cloud award. PernixData made him in 2015 a member of the PernixPro.

7 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

  3. Asim Khan says:

    very good and handy tool
    thanks a lot

  4. Kon says:

    {$_.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.

  5. Joel says:

    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

    • CLAMENT says:

      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.

Leave a reply to psvmware Cancel reply