A quick one :
Trying to find which vms are residing on datastore :”datastore_name” and are located in cluster “your_cluster”
1 2 3 |
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:
1 |
(Get-Datastore -Name 'datastore_name').Extensiondata.Vm|%{(Get-View -Id $_.toString()).name} |
1 2 |
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}} }} 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}}}} |
6 comments
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.
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.
Anyway to export this info to an excel file?
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/
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” ‘