Get-DatastoreStats

Having set up a development lab environment for a project team they began having issues related to storage due to not seemingly understanding that if you put too many Virtual Machines (VMs) on a datastore (LUN) that there might be performance issues. In addition under VMware it is advisable, at least anecdotely, to keep 10 to 15 % free on every LUN. This is too allow startup and restart type VM operations to write temporary files and logs. I have found that having less than 10% free begins to cause problems such as VMs being unable to power up, operations timing out etc.
So what I needed was not only the capacity and free space of a datastore, I also need the number of VMs.

function Get-DatastoreStats
{
  param(
        [Parameter(
            Mandatory=$true,
            ValueFromPipeline=$true,
            HelpMessage="Datastore"
        )]
        [VMware.VimAutomation.Client20.DatastoreImpl]
        $Datastore
  )

  Process {
    Write-Debug "Get a list of VMs on the datastore"
    $vmlist = Get-VM -Datastore $Datastore

    #If no VMs returned then set value to zero
    if ($vmlist -eq $null)
    {
      $NumVMs = 0
    } 
      elseif ($vmlist.count -ne $null)
    {
    	$NumVMs = $vmlist.count
    } else {
    	$NumVMs = 1
    } #end if

    $hash =  @{}
    $hash.Name = $Datastore.Name
    $hash.CapacityGB = [Math]::Round($Datastore.CapacityMB/1KB,0)
    $hash.FreeSpaceGB = [Math]::Round($Datastore.FreeSpaceMB/1KB,0)
    $hash.FreeSpacePct = [Math]::Round($Datastore.FreeSpaceMB/$Datastore.CapacityMB*100,0)
    $hash.NumberVMs = $NumVMs
    $object = new-object PSObject -property $hash
    $object | Select Name, CapacityGB, FreeSpaceGB, FreeSpacePct, NumberVMs

  } # end process

} #end Function

Usage:
Pass datastore objects to the function via the pipeline:
Get-Datastore -Name MyDatastore | Get-DatastoreStats
or
Get-Datastore | Sort | Get-DatastoreStats | ft

Example output of Get-DatastoreStats function

Now I and the development team can see the datastore usage and VM numbers in one little report.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.