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?
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.
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.
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
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
Your post has been moved to the VI Toolkit (For Windows) - Beta forum
Dave Mishchenko
VMware Communities User Moderator
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)
Anyone find a way to list all datastores with such VM's?
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.
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}}
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.
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