VMware Cloud Community
GreyhoundHH
Enthusiast
Enthusiast
Jump to solution

Get all VMs in givencluster on specific datastore

Hi,

I need help filtering all VMs in a given Cluster which are located on specific datastores. What I've go so far:

List all VMs in a given cluster:

Get-Cluster TEST-CLUSTER | Get-VM

List all VMs on given datastores:

Get-VM -Datastore "TEST-DATASTORE"

But when I try to combine these I still get all VMs (also from other clusters) on this datastore:

Get-Cluster TEST-CLUSTER | Get-VM -Datastore "TEST-DATASTORE"

I've also found this command:

Get-Cluster TEST-CLUSTER | Get-VM | % { @{$_.name=$_.datastoreidlist | %{(Get-View -Property Name -Id $_).Name}} }

This lists the datastores for all VMs in the given Cluster. Now I would need to filter this down to a specific datastore.

I need help with either way 😃

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

With a nested Where-clause and some not-so-nice PS trickery it seems to work :smileygrin:

$ds = Get-Datastore Test* | %{$_.ExtensionData.MoRef}

Get-Cluster Test-Cluster | Get-VM | 
where {$vm = $_; $ds | where {$vm.DatastoreIdList -contains $_}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

Doesn't this work for you ?

Get-VM -Datastore Test-Datastore -Location (Get-Cluster Test-Cluster)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
GreyhoundHH
Enthusiast
Enthusiast
Jump to solution

No, this returns all VMs on that datastore and not only VMs in the given cluster..

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

And this one ?

$ds = Get-Datastore Test-Datastore 
Get-Cluster
Test-Cluster | Get-VM | where {$_.DatastoreIdList -contains $ds.ExtensionData.Moref}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

GreyhoundHH
Enthusiast
Enthusiast
Jump to solution

This one seems to work so far.

But I have to add that I would need to find the VMs on several datastores. Like all datastores starting with TEST*.

Using this doesnt seem to work:

$ds = Get-Datastore "TEST*"
Get-Cluster Test| Get-VM | where {$_.DatastoreIdList -contains $ds.ExtensionData.Moref}

But this finds the correct datastores:

Get-Datastore "TEST*"

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

With a nested Where-clause and some not-so-nice PS trickery it seems to work :smileygrin:

$ds = Get-Datastore Test* | %{$_.ExtensionData.MoRef}

Get-Cluster Test-Cluster | Get-VM | 
where {$vm = $_; $ds | where {$vm.DatastoreIdList -contains $_}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
GreyhoundHH
Enthusiast
Enthusiast
Jump to solution

Thanks, works perfectly 😃

Reply
0 Kudos