VMware Cloud Community
lukeglazebrook
Enthusiast
Enthusiast

Urgent report

I Need to urgently export a list of all VM's and their respective Datastore's including LUNS ideally.  Would some kind soul show me a script I can use in PowerGUI.  I may have missed it but I cant find anything that fulfills the requirement.  I have the community PowerPack set and cant seem to find it unless I am blind.

Unfortunately scripting is not my strong point but I hope to address that.

2 Replies
LucD
Leadership
Leadership

Try something like this

&{foreach($vm in (Get-View -ViewType VirtualMachine)){
 
Get-View $vm.Datastore |
 
Select @{N="VM";E={$vm.Name}},Name,@{N="Lun";E={($_.Info.Vmfs.Extent | %{$_.DiskName}) -join ','}}
}}
| Export-Csv report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Tibmeister
Expert
Expert

A slightly different way to "skin the cat" is by using a VIProperty.  I like this because it puts this info into the object for each VM, so if you select one, 20, a filtered list, etc, you have your info.  So, for this, it would be:

New-VIProperty -Name DatastoreList -ObjectType VirtualMachine -Value {get-view -ViewType VirtualMachine -Filter @{"Name" = $args[0].Name} | Select @{N="DataStoreName";E={(get-view $_.Datastore | %{$_.Name}) -join ","}}}

Now you can do a get-vm | Select -ExpandProperty DatastoreList and have all the data there.  If you want to get "fancy", you can also do $vm | select Name, @{N="Datastores";E={($_ |Select -ExpandProperty $DatastoreList) -join ","}}

The second part is a little "clodgey" but works, but for what you are wanting, then you can just do a get-vm | select Name, @{N="Datastores";E={($_ | Select -ExpandProperty DatastoreList | %{$_.DatastoreName}) -join ","}}.  That seems like a lot of work for you direct question, but what if you need down the road to list VM's that are on more than one datastore?  The above code now becomes:

get-vm | where {$_.DatastoreList.Count -gt 1} | select Name, @{N="Datastores";E={($_ | Select -ExpandProperty DatastoreList | %{$_.DatastoreName}) -join ","}}

Like I said, many ways to skin the cat and jsut wanted to throw my hat in on the matter.