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 😃
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
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
No, this returns all VMs on that datastore and not only VMs in the given cluster..
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
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*"
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
Thanks, works perfectly 😃