On occasions a audit needs to take place on VMware to see how much disk usage was being taken up by all the VMs PowerCLI is an ideal tool for working this out, and this is how I went about it.
lets pull a list of all the powered on VM’s. Below we are filtering on “PowerState”
get-vm | Where-object{$_.PowerState -eq "PoweredOn" }
Great now we have a list of Powered on VM’s Next, we need to work out how much disks space they take up. There’s a command “Get-HardDisk” which looks useful, this returns a list of the hard drives connected to these VMs:
get-vm | Where-object{$_.PowerState -eq "PoweredOn" } | Get-HardDisk
And then we can use “Measure-Object” to report on the capacities of these disks and add them together.
(get-vm | Where-object{$_.PowerState -eq "PoweredOn" } | Get-HardDisk | measure-Object CapacityGB -Sum).sum
This returns a number, in gigabytes, of the total size of all the disks of the powered-on VMs on this vCenter. However, most of my VMs are thin-provisioned, so how much space am I actually consuming? Get-HardDisk can’t tell me this, but the “UsedSpaceGB” property of the VM can. The new code looks like this:
((get-vm | Where-object{$_.PowerState -eq "PoweredOn" }).UsedSpaceGB | Measure-Object -Sum).Sum
This produces a number, again in gigabytes, of the total size taken up by all these disks. Just what I needed, except the resulting number includes a lot of decimal places. Last step is to neaten this up by rounding it to the nearest Gigabyte,
[math]::Round(((get-vm | Where-object{$_.PowerState -eq "PoweredOn" }).UsedSpaceGB | measure-Object -Sum).Sum)
