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

by Grzegorz Kulikowski

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


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

Advertisements

You may also like

6 comments

monahancj October 30, 2012 - 7:10 pm

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.

Reply
psvmware October 30, 2012 - 7:23 pm

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.

Reply
Justin April 29, 2014 - 8:30 pm

Anyway to export this info to an excel file?

Reply
psvmware April 29, 2014 - 8:55 pm

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/

Reply
Mike March 10, 2015 - 3:50 pm

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

Reply
psvmware March 11, 2015 - 10:59 am

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” ‘

Reply

Leave a Reply

Chinese (Simplified)DutchEnglishFrenchGermanHindiPolishRussianSpanish