GreyhoundHH
Enthusiast
Enthusiast

Get all VMs in givencluster on specific datastore

Jump to solution

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 😃

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

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

0 Kudos
6 Replies
LucD
Leadership
Leadership

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

0 Kudos
GreyhoundHH
Enthusiast
Enthusiast

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

0 Kudos
LucD
Leadership
Leadership

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

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*"

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
GreyhoundHH
Enthusiast
Enthusiast

Thanks, works perfectly 😃

0 Kudos