PowerCLI

 View Only
  • 1.  Get all VMs in givencluster on specific datastore

    Posted Feb 08, 2013 08:01 AM

    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 =)



  • 2.  RE: Get all VMs in givencluster on specific datastore

    Posted Feb 08, 2013 08:54 AM

    Doesn't this work for you ?

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


  • 3.  RE: Get all VMs in givencluster on specific datastore

    Posted Feb 08, 2013 08:58 AM

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



  • 4.  RE: Get all VMs in givencluster on specific datastore

    Posted Feb 08, 2013 09:20 AM

    And this one ?

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


  • 5.  RE: Get all VMs in givencluster on specific datastore

    Posted Feb 08, 2013 09:29 AM

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



  • 6.  RE: Get all VMs in givencluster on specific datastore
    Best Answer

    Posted Feb 08, 2013 09:57 AM

    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 $_}}


  • 7.  RE: Get all VMs in givencluster on specific datastore

    Posted Feb 08, 2013 10:15 AM

    Thanks, works perfectly =)