Thursday, February 18, 2010

Get VM disks across datastores

I find myself often having to move VMs around onto different datastores (due to upgrades and hardware migrations). I have found the best way to do this is to determine what VM is on what datastore and plan out where I want it to move to. This gets somewhat tedious if you have dozens (or hundreds) of small datastores. Below are 2 powershell scripts I cam across that gather VM/datastore information and outputs it in a fashion easily usable in Excel.

Option 1: (from http://communities.vmware.com/thread/135266)
This is a quick little script that gets all the hosts and the datastores it is on. It runs fairly fast and exports to a CSV file.
get-cluster | %{$cluster = $_; get-vm -Location $_ | %{$vm = $_; $ds = get-datastore -vm $_; echo "$cluster,$vm.name,$ds" >> c:\$cluster.csv ; write-host -NoNewline . }}

Option 2: (from http://doitsmarter.blogspot.com/2008/10/list-vm-usage-and-assosiated-datastore.html)
This is a more invovled script that takes much longer to run. The benefit of this script is that it will include the disk size along with the VM and datastore. This can be very useful when planning migrations because you can easily identify how much space will be needed on the target datastore. NOTE: This only reports the disk capacity, not how much is actually allocated
$datastoreExp = @{N="Datastore"; E={ ($_ | get-datastore | select-object -first 1).Name }}
$diskSizeExp = @{N="Total Disk"; E={ ($_ | get-harddisk | measure-object -property CapacityKB -sum).Sum }}
get-vm | select Name, $datastoreExp, $diskSizeExp | sort -property Datastore,"Total Disk"

No comments: