How to find the number of VMs running on a Datastore Using PowerCLI – VirtuallyThatGuy

I wanted to check the heavy hitters on my Datastores and redistribute the load accordingly. I could use datastore cluster (you need enterprise plus license for this)…. Or alternantively vRealize Operation Manager… but thought why not use powercli? Below is what I came up with… In a larger environment (not my homelab) SDRS is your best bet. I will cover that in a future post.

####​​ Connect to vCenter Server

$vCenters​​ =​​ "uk3p-vc01.lab.local",​​ "hdcp-vc01.lab.local"

 

Connect-VIServer​​ $vCenters

 

####​​ Get Datastores and How many VMs run on them

Get-Datastore​​ |​​ Select​​ Name,​​ @{N="NumVM";E={@($_​​ |​​ Get-VM).Count}},​​ Datacenter,​​ FreeSpaceGB,​​ CapacityGB,​​ Type,Id​​ |​​ Sort​​ Name​​ |​​ Ft​​ -a​​ 

 

#### Get the VMs that run in the those Datastores

Get-Datastore​​ |​​ get-vm​​ |​​ select​​ Name,​​ @{N="Datastore";E={Get-Datastore​​ -VM​​ $_}},​​ @{N="Cluster";E={Get-Cluster​​ -VM​​ $_}}​​ |​​ ft​​ -a

 

​​ 

#### Connect to vCenter Server
$vCenters = "uk3p-vc01.lab.local", "hdcp-vc01.lab.local"

Connect-VIServer $vCenters

#### Get Datastores and How many VMs run on them
Get-Datastore | Select Name, @{N="NumVM";E={@($_ | Get-VM).Count}}, Datacenter, FreeSpaceGB, CapacityGB, Type,Id | Sort Name | Ft -a 

#### Get the VMs that run in the those Datastores
Get-Datastore | get-vm | select Name, @{N="Datastore";E={Get-Datastore -VM $_}}, @{N="Cluster";E={Get-Cluster -VM $_}} | ft -a

You May Also Like

About the Author: VirtuallyThatGuy

Leave a Reply

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