Automation

 View Only
  • 1.  Get object (Clusters and Templates) counts

    Posted Oct 15, 2008 02:03 PM

    I am trying to get the number of clusters & templates per datacenter but .count is returning nothing.

    If i run the commands below I get nothing from each .count. but runinng just the "Get-Cluster -Location $dc" or "Get-Template -Location $dc", returns a list. Is there another way to get these counts or am I doing something wrong. I am using the same logic to get other values such as "(Get-VMHost -Location $dc).count" return the count of host just fine and "(Get-VM -Location $dc).count" gets the count of vm's just fine.

    #Get DataCenter
    $dc = Get-DataCenter -Name "DataCenterName"
     
    #Get Cluster Count - Not working
    (Get-Cluster -Location $dc).count
     
    #Get Template Count - Not Working
    (Get-Template -Location $dc).count
    



  • 2.  RE: Get object (Clusters and Templates) counts

    Posted Oct 15, 2008 03:47 PM

    What you are doing is OK.

    I get values back when I run this.

    Is there something in the variable $dc ?

    Does Get-Cluster without any parameters return anything ?

    What I noticed and what looks strange, if you pass to the -Location parameter an non-initialised variable the cmdlet will just ignore the parameter.



  • 3.  RE: Get object (Clusters and Templates) counts

    Posted Oct 15, 2008 03:57 PM

    $dc appears to have a value since the other .counts work. Below is the complete script, only the template.count and cluster.count doesn't work. the other lines do, thats why I am so confused.. If run the $dc = .... then just "Get-Template -Location $dc" without the count( ) I get a list back

    Running Get-Cluster and Get-Template by themself both return multiple values.

    
     
    #Get DataCenter
    $dc = Get-DataCenter -Name "DataCentername"
     
     
    #Get HostCount - Works
    $hostcount = (Get-VMHost -Location $dc).count
     
     
    #Get VM Count - Works
    $vmcount = (Get-VM -Location $dc).count
     
     
    ## Get Template Count - DOESN'T WORK
    $templatecount = (Get-Template -Location $dc).count
     
     
    #Get Cluster Count - DOESN'T WORK
    $clustercount = (Get-Cluster -Location $dc).count
     
     
    #Get Networks - Works
    $dcview = $dc | %{Get-View $_.ID}
    $networkcount = $dcview.Network.Count
     
     
    



  • 4.  RE: Get object (Clusters and Templates) counts

    Posted Oct 15, 2008 04:01 PM

    Also if it matters.. I am running VMware VI Toolkit for Windows 1.0 build 113525



  • 5.  RE: Get object (Clusters and Templates) counts
    Best Answer

    Posted Oct 15, 2008 04:39 PM

    Could it be that you have only 1 cluster and 1 template in your VC ?

    If I simulate that I see the same behavior.

    The explanation, if you only have 1 template then Get-Template will return a TemplateImpl object.

    If you more than 1 template the cmdlet will return an array of TemplateImpl objects.

    Now an array has the builtin property count, while the TemplateImpl object doesn't.

    The better way to "count" is like this

    (Get-Template | Measure-Object).Count
    



  • 6.  RE: Get object (Clusters and Templates) counts

    Posted Oct 15, 2008 05:14 PM

    Yep that is it, I have 1 cluster and 1 template per datacenter currently. Didn't know it returned a different object if there was only one. Will use the Measure-Object method as you described instead.

    Thanks for the help LucD :smileyhappy: