Locate vms that are in particular cluster and are using particular datastore in powercli


A quick one :
Trying to find which vms are residing on datastore :”datastore_name” and are located in cluster “your_cluster”

Get-Cluster "your_cluster"|Get-vm |?{($_.extensiondata.config.datastoreurl|%{$_.name}) -contains "datastore_name"}
or
Get-Cluster "your_cluster"|Get-vm |?{($_.extensiondata.config.datastoreurl|?{$_.name -eq "datastore_name"})}

It’s easier if the vms reside on particular datastore and you do not care in which cluster they are:

(Get-Datastore -Name 'datastore_name').Extensiondata.Vm|%{(Get-View -Id $_.toString()).name}

And approach where you want to check all datastores in specific cluster:
So i was thinking about this and came out with that 😉 :

get-vmhost -Location "Cluster_01"|get-datastore|%{$ds=$_; $ds.Extensiondata.Vm|%{$_|select @{n='vm name';e={(Get-View -property name -Id $_.toString()).name}},@{n='ds name';e={$ds.name}}  }}

In case you want to check serveral datastores with known names for vms you can do this, assuming that for example names are written in file : c:\dsfile.txt

Get-Datastore -Name (get-content c:\dsfile.txt) | %{$temp=$_; $temp.extensiondata.vm | % { $_ | select @{n='VMName';e={(get-view -property name -id $_).Name}},@{n='ds';e={$temp.name}}}}
Advertisements

6 thoughts on “Locate vms that are in particular cluster and are using particular datastore in powercli

  1. Get-VM -Datastore datastorename | ? { $_.vmhost.parent.name -eq “clustername” }

    It’s a little easier for me to read, but might be slower. I’m not sure. To access “$_.vmhost.parent.name” there’s a hidden “Get-VMHost” being done which will slow it down.

  2. Yep, correct! that’s they beauty of that i guess that you can do thing in several ways. I compared those 2 approaches on some very small cluster, so differences can be larger on bigger environments. my approach 0s 945ms, your approach 1s 613ms . Not a huuuge difference, but like i said it may vary when infra gets larger.

    • If you don’t mind CSV format you can import it with using export-csv cmdlet. For example:
      get-vmhost -Location “Cluster_01″|get-datastore|%{$ds=$_; $ds.Extensiondata.Vm|%{$_|select @{n=’vm name’;e={(Get-View -property name -Id $_.toString()).name}},@{n=’ds name’;e={$ds.name}} }} | export-csv -noTypeInformation c:\file.csv
      and then import it in Excel.
      If for some reason you want to have this in XLS file formatted, colored and so on, it is also possible, but it takes longer to do it if you want to do it yourself. There are some exporters online if you want to use them.
      http://www.itpilgrims.com/2013/01/export-xlsx/

  3. Hi,
    I’m trying to get the first script to work but unless I use the full Datastore name, it returns 0 results for partial Datastore names
    Get-Cluster “your_cluster”|Get-vm |?{($_.extensiondata.config.datastoreurl|%{$_.name}) -contains “datastore_name”}
    so DC1Datastore1 works (the full name) but store1 doesn’t. I’m looking to return all VMs which reside in DC1, there is a DC2Datastore01 I wish to ignore. I’m I typing something wrong somewhere? Thanks!
    Mike

    • Hi Mike, in your line, please change the ‘-contains’ to : ‘-match’ , you can then use ‘datasto’ and it will find everything that matches this, or you can use : ‘-like “datasto” ‘

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s