Wednesday, June 13, 2012

Finding VM disk details in powershell

Occasionally I have to do some VMware datastore maintenance (remove datastores, migrate SANs, etc...) that requires me to move VMs from one DS to another. Traditionally this should be simple: find all VMs on a DS, sVmotion the VMs to a target DS, and remove the original. To make it easier, I rename the datastores as DontUse_XXXX, to easily identify which ones I am clearing, and to keep others from adding VMs to them.
In vSphere 5 this gets even easier: put the DS into maintenance mode and the VMs are migrated automatically.

To complicate things, we have some VMs that are on shared datastores, some that are on dedicated datastores (such as databases), and some that are on both. This means I can't arbitrarily migrate VMs from 1 DS to another DS. If I did this, then database servers would be moved from dedicated RAID 10 LUNs to shared disks without any guaranteed performance.

The first major step is to identify what VMs I have, and how I need to move them. The below powershell script will query all VMs on my DontUse datastores and return the VM name, total size in GB, number of disks, and number of DataStores the VM is on.

get-vm -datastore dontuse* | select Name, @{name="GB";expression={($_ | get-harddisk | Measure-Object -prop CapacityKB -sum).sum /1024/1024}}, @{name="disks";expression={($_ | get-harddisk | measure-object ).count}}, @{name="DS";expression={($_ | get-harddisk| %{($_.filename -split " ")[0]} | sort-object | get-unique).count}}

This quickly alerts me to any VMs I can migrate without issue, and which ones to look deeper into prior to migrating

No comments: