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
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
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
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
I believe I am allready using powershell v 3
$Host.Version
Major Minor Build Revision
----- ----- ----- --------
3 6 0 21
You are right. Even in PowerShell v3 you have to use my trick.
Should I be declaring all my arrays like this?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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.
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
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
Try switching strict mode off, provided it is on of course.
Set-StrictMode -Off
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
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
Set-StrictMode -Off
also solved this problem for me. Thanks!
looks like upgrading to powershell v3 did the trick
After installing powershell 3.0 everything works. Thanks LucD and RvdNieuwendijk
Look like after upgrading to powershell 3.0 everything works. Thanks LucD and RvdNieuwendijk