VMware Cloud Community
DroppedAtBirth
Contributor
Contributor
Jump to solution

Get object (Clusters and Templates) counts

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

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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


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

View solution in original post

Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

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.


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

Reply
0 Kudos
DroppedAtBirth
Contributor
Contributor
Jump to solution

$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
 
 

Reply
0 Kudos
DroppedAtBirth
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
DroppedAtBirth
Contributor
Contributor
Jump to solution

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 Smiley Happy

Reply
0 Kudos