Notes of a scripter

Get free space of all Datastores with PowerCLI

Before I spin up a new VM, I like to check which datastore has the most free space.  This can be completed with a PowerCLI command.

Get-Datastore

The problem with this, it it is too broad. This gets all of the datastores in vSphere, they are not in any sort of order, and it is also getting datastores that i wouldn’t normal use to put a VM on.  Like the datastores that are used just for ISOs and VM templates.

Get-Cluster Cluster_Name | Get-Datastore | sort -Property FreeSpaceGB -Descending

This is starting to pinpoint down to where we need it. It is just getting the datastores that are on the cluster that we are going to put the VM on. It is also sorting the list of datastores based on the FreeSpaceGB property. But it still getting the unnecessary datastores that I wouldn’t use.

Get-Cluster Cluster_Name | Get-Datastore | where {$_.Name -notlike "*ISO*" -and $_.Name -notlike "*template*" } | sort -Property FreeSpaceGB -Descending

This is exactly the list of datastores that I would look at to see where to place a new VM. This filters out the datastores that are local to the VMhosts, and other unnecessary datastores that I would use in the first place. Just swap out the “*ISO*” and “*template*” for the ones in your environment that you don’t to report on.

function Get-FreeSpace ($clustername) {
get-cluster $clustername | Get-Datastore | Where {$_.Name -notlike "*ISO*" -and $_.Name -notlike "*template*" } | sort -Property FreeSpaceGB -Descending
}

I have turned this into a function and added it to my PowerShell profile (link) so I can call it at any time to check the freespace of any given cluster.

The results from running Get-FreeSpace function

 

 

 

 

 

 

You can find this script and all of the other scripts from this blog at https://github.com/NotesofaScripter

– Stuart

Exit mobile version