Automation

 View Only
  • 1.  Get Number of VMs per ESX Host

    Posted Dec 08, 2010 08:43 PM

    I am able to get the count with the following script command but what I would like to do is report a 0 if there are no VMs on a host. Currently it just show nothing if there are no VMs. Any suggestions?

    Get-VMHost | sort -Property Name | Select Name, @{N="VMCount";E={($_ | Get-VM).Count}}



  • 2.  RE: Get Number of VMs per ESX Host

    Posted Dec 08, 2010 08:55 PM

    output the ESX hosts, if they are blank then you can combine the 2 lists, and see ESX hosts with no listed VM's.



  • 3.  RE: Get Number of VMs per ESX Host

    Posted Dec 08, 2010 09:01 PM

    You can't just add an IF statement to the command? I have been trying that and only getting errors.



  • 4.  RE: Get Number of VMs per ESX Host
    Best Answer

    Posted Dec 08, 2010 10:15 PM

    You can add an IF in the expression part.

    Try it like this

    Get-VMHost | Sort-Object Name | Select Name,@{N="VM";E={if(($_ | Get-VM).Count){($_ | Get-VM).Count} else {0}}}
    

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 5.  RE: Get Number of VMs per ESX Host

    Posted Dec 09, 2010 04:09 PM

    If you want to boost the performance of the script you can avoid calling Get-VM like this (slightly modifying the script of LucD):

    Get-VMHost | Sort-Object Name | Select Name,@{N="VM";E={ if ($_.ExtensionData.Vm -ne $null) { $_.ExtensionData.Vm.Count } else {0}}}
    



    -


    PowerCLI development team



  • 6.  RE: Get Number of VMs per ESX Host

    Posted Dec 09, 2010 04:35 PM

    Good suggestion Dimitar and I considered that one also.

    The problem is that this count will also include the templates while with Get-VM its doesn't.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 7.  RE: Get Number of VMs per ESX Host

    Posted Oct 27, 2011 06:06 PM
    
    Get-VMHost | Sort-Object Name | Select Name,@{N="VM";E={ if ($_.ExtensionData.Vm -ne $null) { $_.ExtensionData.Vm.Count } else {0}}}
    

    Could you edit this to count VMs in a folder with a specific name?  Would that include powered on/off/suspended and VMs inside a vAPP inside that folder?

    Thanks!



  • 8.  RE: Get Number of VMs per ESX Host

    Posted Oct 27, 2011 06:27 PM

    Try something like this

    Get-Folder -Name PCs | Select Name,
        @{N="VM";E={(Get-VM -Location $_ | Measure-Object).Count}}


  • 9.  RE: Get Number of VMs per ESX Host

    Posted Oct 27, 2011 07:20 PM

    Thanks!  And let's say I had multiple datacenters with identical folder structures so they are named the same.  Is there any easy way to add them up with this script?   I have to collect monthly stats for each business unit that has VMs in multiple datacenters.



  • 10.  RE: Get Number of VMs per ESX Host

    Posted Oct 27, 2011 07:28 PM

    You could do that like this

    (Get-Folder -Name SLS | Get-VM | Measure-Object).Count
    


  • 11.  RE: Get Number of VMs per ESX Host

    Posted Oct 27, 2011 07:50 PM

    So simple!  Thank you so much.