I ran a simple script against my vCenter to attempt to get # of VMs assigned a certain named tag, and when I run the ".Count" operator on it, the number returns a count 4 more than what it should.
Code:
$vmtags = Get-TagAssignment -Tag "Name-Of-Tag" | Select Entity, Tag
Running just $vmtags returns the proper info -> 13 VMs (objects) assigned this given tag. If I run $vmtags.Count, the number returned is 17. If I attempt to run the same code against a different Tag name, the same behavior occurs...the Count operator shows 4 more than what it should, even tho running the variable alone displays the correct amount of objects (VMs). Can someone explain why this is? Oh, and the Measure-Object cmdlet does the same behavior.
UPDATE: if I remove Select, it both returns the correct objects and the .Count operator rerturns the correct number. Weird. Why is that?
Thank you!
Is it possible to have any other entity in your VC associated with the certain tag that could be returned when invoking that script ?
Good guess, but already checked that too...against DC, Cluster, & DS objects. Nothing returned. These tags are new...haven't used them before (no need to til now). Could it be, since I'm using 'Select' and/or 'FT' parameters that table headings could be counted? Never experienced that behavior before, so I'm just thinking of anything here. Again, when explicitly getting/calling for VMs with a specified tag name, the count is accurate. Only when using Select and/or FT does the count increment (bogusly) by 4. Not sure why.
Appreciate the response and suggestion.
Cheers!
I would suggest to get the vm(s) by the specific tag: Get-VM -Tag <tag_name>
However,
1) could you type here how you get the vms fromyour VC ?
2) could you check which are these 4 extra entities after your filtering by invoking the script ?
Yes, I did get the VM count by invoking the VMs directly by using the Get-VM cmdlet instead of just using the Get-TagAssignment cmdlet, as I mentioned above. And that does return the correct count value. I'm just wondering why when using Get-TagAssignment, when populated in a variable and piping 'Select-Object', returns a larger count value (17 objects) than when getting the VMs directly using Get-VM. The script I used which returns the improper count value is in the initial post. I should also state that when looking in vSphere at the tag, it shows as being associated with only 13 objects there too, which is correct. So, the extra 4 values has something to do with PoSH/PoCLI, I'm guessing. I'm just not sure what, thus why I'm here asking for suggestions/assistance 🙂 In other words, I don't know how to check what the 4 other objects/entities are when using my initial script. When using:
$vmtags = Get-TagAssignment -Tag "Name-of-Tag" | Select Entity, Tag | Sort
Then, entering $vmtags, the amount of VMs returned is correct. BUT....if I do a count operator on this variable, i.e. $vmtags.Count, instead of counting my list of VMs manually, the value is incorrect, by an increment of 4. It should show 13, but instead shows 17. If there's a way to display what else is in the variable besides the VMs, besides just typing $vmtags <ENTER>, I'm up for suggestions on how to do so.
And remember, the script I use which returns the incorrect value when using the Count operator, should return any object when I just type $vmtags <ENTER>, because I don't use Get-VMs with it. So the Get-TagAssignment cmdlet should return any object it's associated with. It is only returning VMs because that's the only vSphere object type I've attached my tags to.
Thanks!