Robert van den Nieuwendijk has a nice post on how to use PowerCLI to find VMs that need consolidation and then how to start consolidation. You can that post here: http://rvdnieuwendijk.com/2012/09/26/use-powercli-to-consolidate-snapshots-in-vsphere-5/
But, as always, I prefer to make a function of these kind of things. A function is much easier to remember than a bunch of parameters and cmdlets.
So here’s the code for a function that will search through the vCentre for VMs that need disk consolidation:
function Start-Consolidation
{
[CmdletBinding()]
Param (
[string]$Location = '*',
[switch]$ListOnly
)
$VMs = Get-VM -Location $Location | Where-Object {$_.Extensiondata.Runtime.ConsolidationNeeded}
if ($ListOnly) {
$VMs
}
else {
$VMs | ForEach-Object {
Write-Host "Starting disk consolidation on $_.Name"
$_.ExtensionData.ConsolidateVMDisks()
Write-Host "Finished disk consolidation on $_.Name"
}
}
}
<#
.Synopsis
Searches vCentre for VMs in need of disk consolidation and starts disk consolidation
.Description
This function searches the entire vCentre, or the given folder,datasenter,cluster,vApp or ResourcePool for VMs that is in need of disk consolidation. It will then start disk consolidation on those that need it
.Parameter Location
If you want to narrow the search to a specific folder,datasenter,cluster,vApp or ResourcePool
.Example
Start-Consolidation
Searches the entire vCentre for VMs that need consolidation and starts consolidating them
.Example
Start-Consolidation -Location vApp01
Searches through vApp01 for VMs that need consolidation and starts consolidation on them
.Link
http://cloud.kemta.net
#>
Any input is much appreciated 🙂
Well I’m using Powersehll 3.0 and PowerCLI 5.5 and when I try to give it a name of a template it fails as well as a V-app. The error is
Get-VM : 02/20/2014 13:26:57 Get-VM Could not find VIContainer with name ‘myVapp’
and also
Get-VM Could not find VIContainer with name ‘myVapp’ when I give it a vapp.
so maybe it doesn’t work on a cloud system?
How to run this function in power cli..? Should I save and run this script or can I direct copy paste in once I connected to Vcenter through powercli
If you paste the entire code in powercli you can call the function later on by running Start-Consolidation