WTF VMware? |
If you have a large environment, this can be time consuming and difficult to track down for all your VMs. However, you can use the power of the command line to save yourself some time.
SSH into the ESX host:
cd /vmfs/volumes/
grep -i 000001.vmdk */*/*.vmx |awk '{print $3}' |sort |uniq |cut -d "\"" -f 2
This will output a list of VMs (and their disks) that are running on a snapshot. You may be surprised by the number of VMs.
Alternatively via PowerCLI (one-liner swiped from here):
foreach($vmview in get-view -viewtype virtualmachine){ $vmview.Config.Hardware.Device | ? {$_ -is [VMware.Vim.VirtualDisk]} | %{$_.Backing | select @{N="VMname";E={$vmview.name}},Filename |?{$_.FileName.Split('/')[-1] -match ".*\-[0-9]{6}\.vmdk"} } }
Oh, you want to remove all the snaps at once? Well, here's my new and improved code, based upon the above.
$DirtyVMs = foreach($vmview in get-view -viewtype virtualmachine){ $vmview.Config.Hardware.Device | ? {$_ -is [VMware.Vim.VirtualDisk]} | %{$_.Backing | select @{N="VMname";E={$vmview.name}},Filename |?{$_.FileName.Split('/')[-1] -match ".*\-[0-9]{6}\.vmdk"} } } $VMlist = $dirtyVMs | select VMname -unique foreach($i in $VMlist) { New-Snapshot -Name DirtySnapCleaner -Description "Temporary Snapshot" -Memory:$false -Quiesce:$false -VM $i.VMname -Confirm:$false Get-Snapshot -vm $i.VMname | remove-snapshot -removechildren -confirm:$false }
Hi there,
ReplyDeletejust few remarks you can use to optimize.
1) From my experience , try to avoid getting vms using their names, this can be tricky when you have env with vms with the same name, use morefs instead
2) There is no need to switch to powercli cmdlets when the whole script was written using vsphere api
3) in regards of that powercli script you have here you could combine the current $vmview with creating + deleting the snapshot using :
$vmview.CreateSnapshot_Task + $vmview.RemoveAllSnapshots_Task
In that way you would be 100% sure that you create and destroy snap chain for that particular vm, and not a vm that has a duplicated name.
at the end, i know how it goes, i mean every administrator knows his env, and probably is aware if he has duplicate names or not, but , just wanted to share my thought.
Great work!
Greg