PowerCLI script to report Guest OS disk usage

[Update, December 2nd, 2014]

You might be also interested in an revised version of this script, that I posted here

[/Update]

Anybody can have a bad day, some people claim it is best to clean one’s desk on a day like that, but I personally prefer to chase customers of my virtual infrastructure for resources they are ab-using.

On many occasions projects request a lot of disk space for their virtual machines and then under-utilize these disks which is just “not cool”, especially if they require thick provisioned vmdks (which I always prefer for servers).

Even if you don’t want to chase anybody for hoarding disk space it is always good to know how much of these hundreds of GBs you provisioned is actually used by Guest OS and applications running inside the virtual machine – this gives you information on how much of disk space can be potentially reclaimed if you really need to clean-up your datastore.
Gathering such information is really easy if you have vCOPS deployed, but for smaller environments you need to do it yourself and this is where the script I’m about to share comes handy.

As with all my scripts –  not really rocket science in here, methodology I use was inspired by Alan Renouf’s post from a few years ago already.

The script requires two parameters, namely vCenter Server name and datastore name (I presume you would like to have such report per datastore) and they are mandatory, so if you forget them while invoking the script you will be asked to provide them before execution starts.

For each vm from datastore we are checking a PowerShell Object is created containing vm name and fields with information about disks which are (in following order): “disk path” (so drive letter in Windows or mount point in Linux), total disk capacity in megabytes, free space in MB and finally we calculate percentage of free space. Every object describing disk configuration of single vm is stored to array and at the end of the script this array is saved to .csv report under file name which (just for convenience) contains name of the datastore. The report is formatted in a way that there is only one line for each vm and columns contain information about each disk.

See example:

The tricky part is that every vm can have different number of disks attached (so number of “disk information” properties can be different per every object we create) whereas Export-CSV cmdlet formats the output based on the first object that is passed to it and resulting .csv file has only as many columns as there were properties defined in the first object.

Translating above to English – if we sorted our vm list alphabetically and the first vm had only single disk – output .csv would have only the number of columns to contain information about this single disk (5 columns in our case) and following vms would inherit this number of columns (so if 2nd vm had 3 disks – only information about 1st disk would be in .csv file, the rest would be just truncated).

To circumvent this issue we sort the collection of vms in descending order so that vm with the largest number of disk is listed first (this is done before script even checks the disks).
The original report looks a bit odd because of that, but you can easily process it further with MS Excel or any other editing tool.

Of course the only way to retrieve information about Guest OS is via VMware Tools, that’s why the script checks if tools are running for each vm (machines without running VMware Tools will not be present in the report!).

This is one of my older scripts and it worked pretty fine for me so far, in fact the only issues I had were with virtualized Solaris x86 servers with Vmware Tools running but in status of being “unsupported” (for whatever reason, I’m not really a Solaris expert, probably some very old VMware Tools were used with these vms).

I hope you find this script useful, feel free to give your opinion on it!

Sebastian Baryło

Successfully jumping to conclusions since 2001.