VMware Cloud Community
michaelbrux
Contributor
Contributor
Jump to solution

cluster.count = $null - Why is this happening ?

I am attempting to capture the number of clusters on a VC and when I run the script on a VC that has only one cluster I am seeing the object set to $null.  If I run the code on a VC that has more than 1 cluster $ccount is set to the correct value.  I am not sure why this is happening and I am hoping someone can help me out.  So here is what I am doing

$clus=Get-Cluster

$ccount=$clus.count

PowerCLI Version

----------------

   VMware vSphere PowerCLI 5.1 Release 2 build 1012425

---------------

Snapin Versions

---------------

   VMWare AutoDeploy PowerCLI Component 5.1 build 768137

   VMWare ImageBuilder PowerCLI Component 5.1 build 768137

   VMware vCloud Director PowerCLI Component 5.1 build 1012427

   VMware License PowerCLI Component 5.1 build 669840

   VMware VDS PowerCLI Component 5.1 build 1012428

   VMware vSphere PowerCLI Component 5.1 build 1012428

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Even better, without tricks, upgrade to PowerShell v3.

In that version even singletons have a Count property.


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

View solution in original post

0 Kudos
18 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

If the vCenter Server has more than 1 cluster the $clus variable is an array object that has the count property. If the vCenter Server has only 1 cluster than the $clus variable isn't an array and doesn't have a count property. You can solve this problem by making it always an array with:

$ccount=@($clus).count

Message was edited by: Robert van den Nieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
LucD
Leadership
Leadership
Jump to solution

Even better, without tricks, upgrade to PowerShell v3.

In that version even singletons have a Count property.


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

0 Kudos
michaelbrux
Contributor
Contributor
Jump to solution

I believe I am allready using powershell v 3

$Host.Version

Major  Minor  Build  Revision

-----  -----  -----  --------

3      6      0      21     

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You are right. Even in PowerShell v3 you have to use my trick.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
michaelbrux
Contributor
Contributor
Jump to solution

Should I be declaring all my arrays like this?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

count.png


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

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

PowerShell v3 Single Cluster count.PNG

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You should only declare arays like this if you want to use the count property. 😉

You can also use the next trick that works in all Powerhell versions:

$clus | Measure-Object | Select-Object -ExpandProperty Count

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Luc, you are right that it is a new PowerShell v3 feature and it should work. For some currently unknown reason it doesn't work for me and also not for michaelbrux.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
LucD
Leadership
Leadership
Jump to solution

count2.png

With PowerCLI 5.1 R2 on a W7 Sp1 64-bit

Could it be that either of you started PowerShell with the '-Version 2' parameter ?


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

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

If you use "Powershell -version 2" then the $host variable will show Version 2.0.

There must be a problem in my PowerShell profile, because if I start PowerShell with "powershell -noprofile" then I don't have this problem. I will try to investigate this problem further.

Update:

I found the problem in my profile. It was caused by the line:

Set-StrictMode -Debug -Version Latest

After removing this line the problem was gone.

Message was edited by: Robert van den Nieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try switching strict mode off, provided it is on of course.

Set-StrictMode -Off



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

0 Kudos
michaelbrux
Contributor
Contributor
Jump to solution

It appears that I have powershell 2.0.  If you run the $host command in powergui it appends its own version #.  I ran $host in the powershell window and saw version 2.0.  I am in the process of getting the creds to install powershell 3.0 and I will let you know the results when it is done.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Thanks, btw $psversiontable is much reliable.

See here for a detailed explanation (it explains what PowerGui does as well)


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

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Set-StrictMode -Off

also solved this problem for me. Thanks!

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
michaelbrux
Contributor
Contributor
Jump to solution

looks like upgrading to powershell v3 did the trick

0 Kudos
michaelbrux
Contributor
Contributor
Jump to solution

After installing powershell 3.0 everything works.  Thanks LucD and RvdNieuwendijk

0 Kudos
michaelbrux
Contributor
Contributor
Jump to solution

Look like after upgrading to powershell 3.0 everything works.  Thanks LucD and RvdNieuwendijk

0 Kudos