PowerCLI: More One Liners

On my previous post where we counted how many VMs were on each host and the cluster they were in I was asked a couple of questions:

1.  How about listing the number of VMs per Resource Pool?

2.  How can we extend this to get the templates as well ?

Ok, easy stuff (I thought).

Lets take them in turn…

Question 1.  How about listing the number of VMs per Resource Pool?

What you need to remember about this is that even if a VM is not in a resource pool it still has resources available to it which have to come from somewhere so in fact what VMware do behind the scenes is have a hidden resource pool called ‘Resources’ where it places the VM when it is not in any other resource pool.

If you remember this then yes, it is quite easy as below:

Get-ResourcePool | Where {$_.Name -ne Resources }| Select Name, @{N=NumVM;E={($_ | Get-VM).Count}} | Sort Name|Export-Csv -NoTypeInformation c:\res-numvm.csv

All we needed to do was to put a Where statement in there to not count the VMs in the ‘pre-made resource pool’

Question 2. How can we extend this to get the templates as well ?

Ahhh, this one is simple I thought, all we need to do is put a count in for Get-Template so the one-liner looks like this:

Get-VMHost |Select @{N=Cluster;E={Get-Cluster -VMHost $_}},Name, @{N=NumVM;E={($_ |Get-VM).Count}}, @{N=NumTemplates;E={($_ |Get-Template).Count}} |Sort Cluster,Name |Export-Csv -NoTypeInformation c:\clu-host-numvm-numtempl.csv

Unfortunately as a Template is basically a special type of VM the cmdlets don’t work like this at the moment, you can not get a list of Templates which exist on a host without delving into the SDK so what I suggest you do is wait until the next version of PowerCLI is released and see if they have fixed this issue. – Current version is PowerCLI V4.0.0.1580

5 thoughts on “PowerCLI: More One Liners

  1. subash

    Hi, This Script looks too good in online. However, I am trying to collect VM and HOST counts of Vcenter by each cluster, So could you please guide me how can we do that within this one-liner script.

    Thanks in advance!!


  2. Antonio

    Thanks, that willskip the problem but not solve… since I have some RPools that was created with Resources same as hidden…. 🙁

  3. Pingback: PowerCLI: Get Running VM’s per VMHost and more « ICT-Freak.nl

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.