VMware Cloud Community
angoletti1
Contributor
Contributor
Jump to solution

HowTo list all VMs and their datastores of the clusters with Powershell?

Hi,

I want to list all VMs with their datastores like:

cluster1

vm1 datastore1

vm2 datastore1

cluster2

vm3 datastore2

How can I do this with the powershell?

Reply
0 Kudos
1 Solution

Accepted Solutions
admin
Immortal
Immortal
Jump to solution

Assuming you just want names, here's a quick-and-dirty way:


get-cluster | %{$cluster = $_; get-vm -Location $_ | %{$vm = $_; $ds = get-datastore -vm $_; write-host $cluster.name $vm.name $ds.name}}

Pseudo-code:


get the clusters
for each cluster {
  get the vms
  for each vm {
    get the datastore
    write out a line listing cluster name, vm name, datastore name
  }
}

I like it when the pseudo-code is longer than the code. Smiley Happy

View solution in original post

9 Replies
admin
Immortal
Immortal
Jump to solution

Assuming you just want names, here's a quick-and-dirty way:


get-cluster | %{$cluster = $_; get-vm -Location $_ | %{$vm = $_; $ds = get-datastore -vm $_; write-host $cluster.name $vm.name $ds.name}}

Pseudo-code:


get the clusters
for each cluster {
  get the vms
  for each vm {
    get the datastore
    write out a line listing cluster name, vm name, datastore name
  }
}

I like it when the pseudo-code is longer than the code. Smiley Happy

angoletti1
Contributor
Contributor
Jump to solution

Perfect, that's exactly what I want :smileygrin:

Do you have a blog or something like that, where I can find more examples, to get deeper in the vipowershell?

Best regards

angoletti1

Reply
0 Kudos
admin
Immortal
Immortal
Jump to solution

There is a blog at http://blogs.vmware.com/vipowershell/. You should probably check out the forum, too, which is probably a better place to post questions like this: http://communities.vmware.com/community/developer/windows_toolkit

Reply
0 Kudos
Dave_Mishchenko
Immortal
Immortal
Jump to solution

Your post has been moved to the VI Toolkit (For Windows) - Beta forum

Dave Mishchenko

VMware Communities User Moderator

Reply
0 Kudos
aw443
Contributor
Contributor
Jump to solution

Adias,

This command works great to output the datastore, per VM. However, there is a problem if the VM has separated VMDK's. For those VM's, it doesn't list ANY of it's datastores (and unfortunately in my environment, close to 40% of the VM's have storage on multiple datastores) Smiley Sad

Anyone find a way to list all datastores with such VM's?

Reply
0 Kudos
astrolab
Contributor
Contributor
Jump to solution

How do I export it to a file? I've tried export-csv c:\logs\export.csv and > c:\logs\export.csv with no luck, files are created but empty.

Reply
0 Kudos
mcorey3
Contributor
Contributor
Jump to solution

Is there any formatting needed for this script to work? I copy and paste this into my powergui editor and get an error.

get-cluster | %{$cluster = $_; get-vm -Location $_ | %{$vm = $_; $ds = get-datastore -vm $_; write-host $cluster.name $vm.name $ds.name}}

Reply
0 Kudos
ronnyholtmaat
Contributor
Contributor
Jump to solution

Just change the $ds.name to $ds and it will tell ya all the storage connected.

get-cluster | %{$cluster = $_; get-vm -Location $_ | %{$vm = $_; $ds = get-datastore -vm $_; write-host $cluster.name "," $vm.name "," $ds}}

Greetz

Ronny.

Reply
0 Kudos
ronnyholtmaat
Contributor
Contributor
Jump to solution

Get-VIServer -server <your virtual center host>

get-cluster | %{$cluster = $_; get-vm -Location $_ | %{$vm = $_; $ds = get-datastore -vm $_; echo "$cluster,$vm.name,$ds" >> c:\$cluster.csv ; write-host -NoNewline . }}

Outputs a comma seperated file with the name of the clusters to c:\

You will need a C drive for this script to function

Reply
0 Kudos