PowerCLI: One-Liner to get VMs, Clusters, ESX Hosts and Datastores


image

With the one-liner below, you’re able to create an overview of your VM’s, Clusters, ESX Hosts and Datastores.

Get-VM | Select Name, @{N="Cluster";E={Get-Cluster -VM $_}}, `
@{N="ESX Host";E={Get-VMHost -VM $_}}, `
@{N="Datastore";E={Get-Datastore -VM $_}} 

The following output will be generated:

image

If you add an extra line with the export-csv cmdlet, you can export the output to a CSV file.

Get-VM | Select Name, @{N="Cluster";E={Get-Cluster -VM $_}}, `
@{N="ESX Host";E={Get-VMHost -VM $_}}, `
@{N="Datastore";E={Get-Datastore -VM $_}} | `
Export-Csv -NoTypeInformation C:\Scripts\VM_CLuster_Host_Datastore.csv 

If you want, you can import the CSV file into Excel. Excel has some basic filtering options, so you’re able to filter on ESX Host, Cluster etc.

image

Advertisements

13 thoughts on “PowerCLI: One-Liner to get VMs, Clusters, ESX Hosts and Datastores

  1. Pingback: uberVU - social comments

  2. Dumas Philippe

    Hi,

    Thanks a lot for this script. It will save me a lot of time to make a report of all my vms and their location.

    Thanks for sharing your job with us.
    God Bless You.

    Reply
  3. Pingback: powerCLI to get VM, cluster, Host, and datastore « Cyclops' Blog

  4. vinod

    This is great script. It helped me a lot.
    Additionally can you suggest how to get Total and free space details against each datastore.

    Thanks!

    Reply
  5. ravi

    For example , storage team allocated lun, but that lun not added any cluser and esx, how to collect not added lun details for entire vc each cluser name wise ?

    Reply
  6. David

    That’s awesome but how do I get the datastores on separate lines? I want to use a vlookup table within Excel to match datastores against my storage arrays. Having the datastores concatenated (e.g., lun1, lun 2, lun 3) makes that impossible without manually separating them.

    Reply
  7. leaderone2

    I found the script run very slow, I have 40 ESX host, and about 1000 vms. I run this script for about 40 minutes, Could it run more quickly? If I donot care the Performance consume on the vCenter or the client I run the scripts. Thanks!

    Reply
    1. Jonathan

      I don’t know powershell, but it’s definitely spawning internal loops that are redundant for the “Get-Cluster -VM” filters. If you make them explicit it’s much faster:
      foreach ($cluster in Get-Cluster)
      {
      $hosts = $cluster | Get-VMHost
      foreach ($each_host in $hosts)
      {
      $vms = $each_host | Get-VM
      foreach ($vm in $vms)
      {
      … etc

      You can probably condense the enumeration of each child object to occur within the “foreach” line, but I don’t know how.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s